Script that checks for previous instances running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script that checks for previous instances running
# 1  
Old 06-08-2007
Script that checks for previous instances running

Hello,
I'm trying to write a script that checks for previous instances of the same script which may still be running (this script is scheduled to run every 30 minutes). I want to somehow use the pid from each instance to make sure the previous one isn't running before continuing with my processing, and wait for a set time period before checking again. I'd also like it to only retry a certain number of times before exiting with a failure notification.

I've never used a scripts pid before but one of my unix admins said that I can get the pid of the process by running a "echo $$" within the script.

Any suggestions to be appreciated. Right now I just have an e-mail notification at the end of my script with a date stamp so I can see (every 30 minutes thru the business day) that the script finished before the next one was scheduled to start - which is far from idea. Smilie



EDIT: I should note that this runs on AIX 5.3 and using ksh.

Last edited by bd_joy; 06-08-2007 at 09:49 AM.. Reason: Additional Info
# 2  
Old 06-08-2007
One way that does not use a pid - assuming the scrpit filename is somewhat unique:
Code:
ps -ef | grep ' <scriptfile> ' | grep -v ' <scriptfile >'

A simpler way to is to have a gentleman's agreement on a lock file, if this script has a very low probablility of failure.

When your script starts it looks in a "fixed file name" for 'I finished'. If it finds that, then it changes the file to read 'I am running', runs, then on exit resets the file to 'I finished'.

On startup, if the script finds 'I am running', it can exit with a note in the log file saying the other guy was still running. It can also wait, then check again as many times as you want.
# 3  
Old 06-08-2007
look into 'man fuser' if you have one on AIX.
# 4  
Old 06-13-2007
Quote:
Originally Posted by vgersh99
look into 'man fuser' if you have one on AIX.
I'm not sure what more this shows between the two:

$ ps -ef |grep myscript.sh |grep -v grep
djoy3 606700 688184 0 09:27:57 pts/0 0:00 /bin/sh ./myscript.sh
$ fuser -u myscript.sh
myscript.sh: 606700(djoy3)

They both output the pid.

I guess there really isn't an easy way to do this. I was trying to avoid using start and end trigger files. Does anyone have any other suggestions on this? Thanks for your help!
# 5  
Old 06-13-2007
I'm confused - you wanted a pid, I thought.

Instead of telling us how you want to do it, like find a pid, how about file existence?
Code:
#!/bin/ksh
if [[ ! -f /tmp/myscript.sh.lock ]] ; then  # is file not there 
    echo "1" >  /tmp/myscript.sh.lock   # no, make one and proceeed with script
else
    echo "Another myscript still running."  # exit, file is still there
    exit 1
fi
# ............. do stuff
rm -f /tmp/myscript.sh.lock     # remove the file so the next gut can run
exit 0

I do not see why an approach like this will not work. It does have issues if your script fails a lot or if you reboot during the time the script is running. But. You can easily add an ignore option, myscript.sh -I, so that it runs regardless of the exitence of the file.
# 6  
Old 06-13-2007
Quote:
Originally Posted by jim mcnamara
I'm confused - you wanted a pid, I thought.

Instead of telling us how you want to do it, like find a pid, how about file existence?
Code:
#!/bin/ksh
if [[ ! -f /tmp/myscript.sh.lock ]] ; then  # is file not there 
    echo "1" >  /tmp/myscript.sh.lock   # no, make one and proceeed with script
else
    echo "Another myscript still running."  # exit, file is still there
    exit 1
fi
# ............. do stuff
rm -f /tmp/myscript.sh.lock     # remove the file so the next gut can run
exit 0

I do not see why an approach like this will not work. It does have issues if your script fails a lot or if you reboot during the time the script is running. But. You can easily add an ignore option, myscript.sh -I, so that it runs regardless of the exitence of the file.
Thank you for your input. This is what I think I'm going to do. I can't get the counter to work right, but I'll keep messing with it.

#!/bin/sh
EMAIL1="djoy3@nomail.com"
count=0
until ls myscript2.sh.trg >>/dev/null 2>&1
do
count=count+1
if [ count -gt 16 ]
then
echo "Max wait time exceeded for myscript2.sh job" | mailx -s "myscript2.sh job error - `date`" ${EMAIL1}
exit 1
fi
sleep 20
done
#do other processing here
echo "Other processing running"
touch myscript2.sh.trg
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script function which checks if itself is already running

Hi All, I have a cron job set up which is set to run every 10 seconds. What I need to do is have the script do a check to see if it is already running such that if it is running it wont fire up additional instances and processes according to its normal process. For example if I have a script... (4 Replies)
Discussion started by: landossa
4 Replies

2. Shell Programming and Scripting

Script to do the following checks

Hi , I need a script for processing below scenario. I have to check daily by doing ftp IP to check it is logging or not. So i want this activity to be automated such that if login succesful i will get "FTP LOGIN SUCCESS" in a log file and if fails i want the error message in the same log... (1 Reply)
Discussion started by: sv0081493
1 Replies

3. Shell Programming and Scripting

Script to performs checks

Hi , I need a script which performs below activity I have one file named "testfile" in 9 different directories with same name. I want to perform below action with each testfile of each directory. if ; then mv listfiles listfiles_`date +%b%y` else echo No Such files fi ... (4 Replies)
Discussion started by: sv0081493
4 Replies

4. Shell Programming and Scripting

A script that kills previous instances of itself upon running not killing child processes

I'm likely going to explain this clumsily, so apologies in advance: I have the following script: #!/bin/bash pidPrefix="logGen" checkPrime () { if /sbin/ifconfig eth0:0|/bin/grep -wq inet;then isPrime=1;else isPrime=0;fi } killScript () { /usr/bin/find /var/run -name... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

5. Shell Programming and Scripting

Script counting instances of software running on a machine

Hello to all @here, as Iīm new to this forum, I will try to start in a easy way for my first post. Iīm not beginner in scripting, but also not a proffessional. So please keep easy, if I donīt understand your explanation at once ;) I donīt mean it in a bad way! Here is the Problem: There were... (2 Replies)
Discussion started by: muogli
2 Replies

6. Shell Programming and Scripting

[Bash] MD5 Checks with Script.

Hi. I'm triyng to make a Bash Script that checks (recursively) the MD5 from all the files in a certain directory and compare them against some other check that should be already done and saved in a file. I've reached to the point where i have the MD5 from the file and the MD5 that the script... (1 Reply)
Discussion started by: BiFo
1 Replies

7. Shell Programming and Scripting

Script to perform record format checks

Hi All, I have a requirement to perform the following checks. Input file is a "|" delimited file and looks like this. A|c1|c2|c3|.... B|G1|G2|G3.... C|H1|H2|H3... A|c4|c5|c6|.... B|G4|G5|G6.... C|H4|H5|H6... Now the check is to see if all the "A" records have a corresponding B... (7 Replies)
Discussion started by: gsjdrr
7 Replies

8. Shell Programming and Scripting

Script stops running the remaining checks after becoming admin

Hi all, I encountered a problem where my script stops running the remaining checks after becoming an admin that is written within the script. For example: ========================================= #!/bin/sh check 1 # Runs successfully check 2 # Runs successfully /com/bin/admin #... (1 Reply)
Discussion started by: seanchew
1 Replies

9. Shell Programming and Scripting

Want to make a script that checks connection protocol

Hello all, I currently connect to several servers multiple times a day. Most of the time I connect via SSH through the terminal emulator poderosa (my personal favorite), but sometimes I connect via telnet through xstart because I need it to export a GUI. What I want to do is add something to... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

10. Shell Programming and Scripting

How to limit the number of running instances of a script?

I would like to allow only one instance of a script to run at any moment. I've tried the following solution to count the instances but the result is always the number of running instances plus one and I can't find the problem ps -ef | grep $0 | sed '/^$/ d' | sed '/grep/ d' | wc -l Please... (2 Replies)
Discussion started by: oti
2 Replies
Login or Register to Ask a Question