Script calling multiple scripts (using lock file)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script calling multiple scripts (using lock file)
# 1  
Old 05-23-2017
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.

Code:
#!/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
Code:
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
Code:
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)

Last edited by nms; 05-23-2017 at 02:32 PM..
# 2  
Old 05-23-2017
Change this:
Code:
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.
Code:
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.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 05-24-2017
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
This User Gave Thanks to nms For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling multiple jobs from single shell script

Hi all, I have developed a shell script where I am executing datastage jobs.This script will be further placed in enterprise scheduler to trigger it at particulat time. I have around 300 jobs , I am passing jobnames to script as a paramere. So is it possible I can trigger all jobs from one... (3 Replies)
Discussion started by: prasson_ibm
3 Replies

2. Shell Programming and Scripting

Calling multiple scripts from another scripts

Dear all, I am working on script which call other shell scripts in a loop but problem is from second script am not able to come out. Here is the snippet:- #!/bin/bash HSFILE=/root/Test/Components.txt LOGFile=/opt/domain/AdminDomain/application/logs... (3 Replies)
Discussion started by: sharsour
3 Replies

3. Shell Programming and Scripting

No lock file: Preventing multiple instance of a script

I've been bitten by using a lock or pid file to prevent multiple instances of a script. A user typed kill -9, and the pid file didn't go away. You can't trap -9. So when he tried to restart, it said "already running", and I got trouble report. Argh. So here's what we came up with: # Stop if... (1 Reply)
Discussion started by: McFadden586
1 Replies

4. Shell Programming and Scripting

calling script with multiple variables in second script

Hi All, Just give me an idea on how to do the below logic. 1. I have one master script masterload.sh, the usage of this script is a. masterload.sh FULL BFLF_LOAD.txt b. masterload.sh DELTA TDLD_LOAD.txt c.masterload.sh USER MAS_LOAD.txt FULL , DELTA ,USER are the varaibles based... (1 Reply)
Discussion started by: mora
1 Replies

5. Shell Programming and Scripting

Calling scripts from other script.

I need to call 3 different shell scripts from 2 different scripts, one is a perl script and other is Shell script. In Case -1 : The perl script is myperlscript.pl and the name of three shell scripts which need to be called from the perl script are a1.sh, a2.sh and a3.sh. Each shell script... (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

6. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

7. Shell Programming and Scripting

Calling scripts within script

Hi, I have written a some six scripts to move large files and re-size them. This has been done step by step, taking backup, creating the new files, merging the files, removing the temporary files created. Since these files are around 500 MB, each step takes somewhere between 1 to 5 mins. ... (1 Reply)
Discussion started by: baanprog
1 Replies

8. Shell Programming and Scripting

Calling Multiple Scripts - stops after 1

I have created a script to call 2-3 shell scripts to be execute in succession, however, it seems that after the first shell script completes, the entire script exits out. Example: 1stJob.sh 2ndJob.sh 1st Job - FTP files from Mainframe to Unix using the following commands at the tail of... (1 Reply)
Discussion started by: CKT_newbie88
1 Replies

9. Shell Programming and Scripting

script calling other scripts hangs

I have a script that calls several other scripts in a specified order: # Loop over actions in specified order (STOP_ORDER or START_ORDER) and build and evaluate commands for command in $(eval print '$'${action}_ORDER) do printf "`date`\tExecuting ${action}_${command} = `eval print... (1 Reply)
Discussion started by: rein
1 Replies

10. Shell Programming and Scripting

Calling SQL scripts through Shell Script

Oracle and Scripting gurus, I need some help with this script... I am trying to add the query SELECT * FROM ALL_SYNONYMS WHERE SYNONYM_NAME = 'METADATA' in the current script.... Read the result set and look for the TABLE_NAME field. If the field is pointing to one table eg.... (18 Replies)
Discussion started by: madhunk
18 Replies
Login or Register to Ask a Question