
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!
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon
Categorized:
Shell Script Programming
(Article 6949)
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!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 I have a lot to say, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2000+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|