Home > database >  Detect if script is already running in bash script, and only restart if not
Detect if script is already running in bash script, and only restart if not

Time:06-08

I'm trying to write a script that will check if a script is already running, and not run it on cron if its still going from the last run. I found another post on here where they suggested using:

echo `pgrep -f $0` . "!=" . "$$";
if [[ `pgrep -f $0` != "$$" ]];

While this seems to work when I run it manually in SSH, it gives weird results when run via cron:

14767 14770 . != . 14770

Is this because there are 2 processes running with 2 different pids?

I have come up with this as an alternative:

if [ -n "$(ps -ef | grep -v grep | grep 'run.sh' | wc -l)" > 2 ];
then
    echo "already running"
else
  # do some stuff here
fi

Running the command on its own seems to work as expected:

# ps -ef | grep -v grep | grep 'run.sh' | wc -l)
2

But when in the code, it always shows "already running" , even though my condition is not met:

bash run.sh
2
already running

Maybe I'm doing something wrong with the variable as an int?

UPDATE: As suggested, I am trying flock:

#!/bin/bash

[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :

#... rest of code here

But I get:

flock: failed to execute run.sh: No such file or directory

CodePudding user response:

You could write your code like that but it will be complex and errorprone. Better to use file-locking. The flock command exists for this. Its man-page provides various examples you can cut and paste, including:

#!/bin/bash

[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :

# ... rest of code ...

This is useful boilerplate code for shell scripts. Put it at the top of the shell script you want to lock and it'll automatically lock itself on the first run. If the env var $FLOCKER is not set to the shell script that is being run, then execute flock and grab an exclusive non-blocking lock (using the script itself as the lock file) before re-execing itself with the right arguments. It also sets the FLOCKER env var to the right value so it doesn't run again.

man flock for details.

  •  Tags:  
  • bash
  • Related