How to check if another instance of my shell script is running

后端 未结 15 1989
执念已碎
执念已碎 2020-12-01 00:35

GNU bash, version 1.14.7(1)

I have a script is called \"abc.sh\" I have to check this from abc.sh script only... inside it I have written f

相关标签:
15条回答
  • 2020-12-01 01:10

    Someone please shoot me down if I'm wrong here

    I understand that the mkdir operation is atomic, so you could create a lock directory

    #!/bin/sh
    lockdir=/tmp/AXgqg0lsoeykp9L9NZjIuaqvu7ANILL4foeqzpJcTs3YkwtiJ0
    mkdir $lockdir  || {
        echo "lock directory exists. exiting"
        exit 1
    }
    # take pains to remove lock directory when script terminates
    trap "rmdir $lockdir" EXIT INT KILL TERM
    
    # rest of script here
    
    0 讨论(0)
  • 2020-12-01 01:11

    This is compact and universal

    # exit if another instance of this script is running
    for pid in $(pidof -x `basename $0`); do
       [ $pid != $$ ] && { exit 1; }
    done
    
    0 讨论(0)
  • 2020-12-01 01:12

    Use the PS command in a little different way to ignore child process as well:

    ps -eaf | grep -v grep | grep $PROCESS | grep -v $$
    
    0 讨论(0)
  • 2020-12-01 01:13

    An easier way to check for a process already executing is the pidof command.

    if pidof -x "abc.sh" >/dev/null; then
        echo "Process already running"
    fi
    

    Alternatively, have your script create a PID file when it executes. It's then a simple exercise of checking for the presence of the PID file to determine if the process is already running.

    #!/bin/bash
    # abc.sh
    
    mypidfile=/var/run/abc.sh.pid
    
    # Could add check for existence of mypidfile here if interlock is
    # needed in the shell script itself.
    
    # Ensure PID file is removed on program exit.
    trap "rm -f -- '$mypidfile'" EXIT
    
    # Create a file with current PID to indicate that process is running.
    echo $$ > "$mypidfile"
    
    ...
    

    Update: The question has now changed to check from the script itself. In this case, we would expect to always see at least one abc.sh running. If there is more than one abc.sh, then we know that process is still running. I'd still suggest use of the pidof command which would return 2 PIDs if the process was already running. You could use grep to filter out the current PID, loop in the shell or even revert to just counting PIDs with wc to detect multiple processes.

    Here's an example:

    #!/bin/bash
    
    for pid in $(pidof -x abc.sh); do
        if [ $pid != $$ ]; then
            echo "[$(date)] : abc.sh : Process is already running with PID $pid"
            exit 1
        fi
    done
    
    0 讨论(0)
  • 2020-12-01 01:13

    I didn't want to hardcode abc.sh in the check, so I used the following:

    MY_SCRIPT_NAME=`basename "$0"`
    if pidof -o %PPID -x $MY_SCRIPT_NAME > /dev/null; then
        echo "$MY_SCRIPT_NAME already running; exiting"
        exit 1
    fi
    
    0 讨论(0)
  • 2020-12-01 01:15

    Here's how I do it in a bash script:

    if ps ax | grep $0 | grep -v $$ | grep bash | grep -v grep
    then
        echo "The script is already running."
        exit 1
    fi
    

    This allows me to use this snippet for any bash script. I needed to grep bash because when using with cron, it creates another process that executes it using /bin/sh.

    0 讨论(0)
提交回复
热议问题