|
How can my shell script test to see if it's already running?I have a script that does an ftp from a SCO UNIX server to windows server to get files from the windows server. The script is set in the cron to run every xx minutes. Sometimes the script will hang and leave a process running. This can bog down the UNIX server. I want to be able to do two things. 1. I want to make sure that the script doesn't hang - so it should terminate after xx minutes (the files are very small that it is getting). And 2. I want the script to test to make sure that it is not already running, before starting again. THANKS A classic solution to this is to create a "semaphore" file, something like this at the beginning of the script: cat $$ > /tmp/program.lock
then at the end of the script you delete it: rm /tmp/program.lock
(or, if you want to be more fancy, use the trap command to specify that on exit condition 0 the temp file should be deleted: "trap "rm /tmp/program.lock" 0") One more nuance is that it needs to test to see if the file already exists, and decide what to do if the process is still running. That can be done with a straightforward test: if [ -f /tmp/program.lock ] ; then
# the lock file already exists, so what to do? if [ "$(ps -p `cat /tmp/program.lock` | wc -l)" -gt 1 ]; then # process is still running echo "$0: quit at start: lingering process `cat /tmp/program.lock`" exit 0 else # process not running, but lock file not deleted? echo " $0: orphan lock file warning. Lock file deleted." rm /tmp/program.lock fi fi Alright, that's not entire straightforward, but I think you can see how I would try to solve this problem. If you believe that XX minutes later the script SHOULD be done and the process should be killed if it's still running, then you can do something like this: kill -HUP `cat /tmp/program.lock`
or, if you want to be more aggressive, use: kill -KILL `cat /tmp/program.lock`
To have a kill timer on the script, btw, write a separate little script that just waits xx seconds using the sleep command, then tries to do the kill shown above. Either it'll error out, in which case the script is no longer running, or it'll kill the script. Hope that helps you out!
Categorized:
Shell Script Programming
(Article 6949,
Written by Dave Taylor)
Tagged: ftp, linux, shell programming, shell script, unix Previous: Why do people buy company stock? Next: Why do I have invisible email messages in MySpace? Subscribe!
I think this text in general is usefull, however, be very carefull (or actualy do not do it at all!) kill processes without checking if it is the correct process. Another process might have taken your process ID and you may kill the wrong process, a check if the process is the program you are running should be done at all times!) Do you mean "echo $$" rather than "cat $$"? Posted by: Ken at May 7, 2007 5:21 PMKen, you're right! That should indeed be "echo $$" not "cat $$". Mea culpa! :-) Posted by: Dave Taylor at May 7, 2007 7:47 PMWell, a rather different problem is the following: We want a program, say "perl /home/me/mydir/myscript.pl" to run continously, and to be restarted should it be stopped by a system failure and myscr reads however, this will respawn when myscr dies, not when the perl script dies. Consequently, it will Alternative, script around pidof. Should man pidof to verify behavior on your system. I prefer never to /dev/null any program, but pidof doesn't like to be quiet. Example (bashisms intended!): #!/bin/bash me=$(basename $0)
echo $me sleep 300 I have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
Recommended
Recent Entries
Search
I Need Help!
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and RSS Feeds Building Web Site Traffic Business and Management CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above Facebook Help Google Plus Help HTML and CSS Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Mac OS X Help Pay Per Click (PPC) Advertising Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help WordPress Help |