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)