Script calling multiple scripts (using lock file)

Hi all,

i have compiled a script as below. Basically this script is intended to call 3 other scripts, which i intend to run simultaneously.

#!/usr/bin/bash

HOMEDIR=/path/of/home
LOCKDIR=$HOMEDIR/lock

#check for LOCK
LOCKFILE=$LOCKDIR/$(basename $0 .sh).lock
if [ -f ${LOCKFILE} ]; then
    echo "Script is already running"
    exit
fi

echo $$ > ${LOCKFILE}

#script1
$HOMEDIR/script1 1>>$HOMEDIR/logs/script1.log 2>&1 &
script1=$!

#script2
$HOMEDIR/script2 1>>$HOMEDIR/logs/script2.log 2>&1 &
script2=$!

#script3
$HOMEDIR/script3 1>>$HOMEDIR/logs/script3.log 2>&1 &
script3=$!

#Remove LOCK, but wait that all scripts has finished first
kill -0 $script1 $script2 $script3 >/dev/null 2>&1

if [ $? -eq 0 ]; then
        wait $script1
        wait $script2
        wait $script3
        rm -f ${LOCKFILE}
elif [ $? -eq 1 ]; then
rm -f ${LOCKFILE}
fi

in order to be sure that one instance of the master script runs, i introduced a lock file and then delete it once it is finished, i.e after the 3 scripts are done.

Before removing the lock file I am using the

kill -0

command in order to see if the process of the 3 scripts still exist, hence still running.
Depending on the result of the kill command i then used

wait

then followed by removal of the lock file.
Is this theory correct or maybe you can suggest another way?

(Note: of course the 3 scripts can run in cron without using a master script, but i am restricted in using a master script, so the idea is to keep it that way)

Change this:

if [ $? -eq 0 ]; then
        wait $script1
        wait $script2
        wait $script3
        rm -f ${LOCKFILE}
elif [ $? -eq 1 ]; then
rm -f ${LOCKFILE}
fi

To something like this.

status=$?
if [ $status -eq 0 ]; then
        wait  # will wait for all children
        rm -f ${LOCKFILE}
fi
[ $status -ne 0 ]  &&  rm -f ${LOCKFILE}

The $? variable cannot be "reused" the way you did - store it in a variable instead. Once the if gets evaluated $?
Plus, the wait command [no argument ] will wait for all children.
If a child fails, wait returns the exit code of the child which, in theory, can be any number (1-255) except 0.
So, you can safely check for a non-zero return.

Also note this important fact:
rm removes the directory entry for a file that is open by another process, when that other process exits the physical file is removed from the file system.
A hung child process in a lockfile design can make the script fail and confuse the heck out of you.

1 Like

Hi jim,

i replaced the part with your's, it also worked. Did not know that actually the wait command wait for the "sub" processes with the main script. it's also much cleaner.

Thanks for you input

1 Like