Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

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


Dave's Answer:

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    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

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!)

Posted by: patrick at April 27, 2007 7:14 AM

Do you mean "echo $$" rather than "cat $$"?

Posted by: Ken at May 7, 2007 5:21 PM

Ken, you're right! That should indeed be "echo $$" not "cat $$". Mea culpa! :-)

Posted by: Dave Taylor at May 7, 2007 7:47 PM

Well, 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
, a reboot, a power outage or whatever.
So we put in /etc/inittab a line like
myscr:3456:respawn:/home/me/myscr

and myscr reads
"cd /home/me/mydir;
perl /home/me/mydir/myscript.pl > /dev/null &"

however, this will respawn when myscr dies, not when the perl script dies. Consequently, it will
respawn zillions of perl /home/me/mydir/myscript.pl processes and
eventually put the system to a standstill.
What needs to be done is to check if myscript.pl
is running and only if it is not call it.
In this case I doubt we can use the solution posted above and hence avoid doing a ps -ef | grep myscript.pl and need to excluded the grep from there), and I am not sure what the most elegant way to do the checking would be

Posted by: santi at October 25, 2007 7:29 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.