Sponsored Content
Top Forums Shell Programming and Scripting Script calling multiple scripts (using lock file) Post 302997968 by nms on Tuesday 23rd of May 2017 01:13:49 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SHLOCK(1)						      General Commands Manual							 SHLOCK(1)

NAME
       shlock - create lock files for use in shell scripts

SYNOPSIS
       shlock -p pid -f name [ -b ] [ -u ] [ -c ]

DESCRIPTION
       Shlock  tries  to  create  a  lock  file named name and write the process ID pid into it.  If the file already exists, shlock will read the
       process ID from the file and test to see if the process is currently running.  If the process exists, then the file will not be created.

       Shlock exits with a zero status if it was able to create the lock file, or non-zero if the file refers to currently-active process.

OPTIONS
       -b     Process IDs are normally read and written in ASCII.  If the ``-b'' flag is used, then they will be written as  a	binary	int.   For
	      compatibility with other systems, the ``-u'' flag is accepted as a synonym for ``-b'' since binary locks are used by many UUCP pack-
	      ages.

       -c     If the ``-c'' flag is used, then shlock will not create a lock file, but will instead use the file to see if the	lock  is  held	by
	      another  program.   If  the  lock  is  valid, the program will exit with a non-zero status; if the lock is not valid (i.e., invoking
	      shlock without the flag would have succeeded), then the program will exit with a zero status.

EXAMPLES
       The following example shows how shlock would be used within a shell script:
	      LOCK=/var/run/innd/LOCK.send
	      trap 'rm -f ${LOCK} ; exit 1' 1 2 3 15
	      if shlock -p $$ -f ${LOCK} ; then
		  # Do appropriate work
	      else
		  echo Locked by `cat ${LOCK}`
	      fi

HISTORY
       Written by Rich $alz <rsalz@uunet.uu.net> after a description of HDB UUCP locking given by Peter Honeyman.  This  is  revision  1.9,  dated
       1996/10/29.

																	 SHLOCK(1)
All times are GMT -4. The time now is 06:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy