Check for Linux process in script

k3vsmith
k3vsmith used Ask the Experts™
on
I have an existing linux shell script that is running every so many hours.  

Basically the script itself takes about 1.5 hours to complete. But there can be certain situations where it can take longer. Rather than spacing the script out further between runs I want to only handle that situation. Where if script time executes and there is a previous run still running the new one will just exit and let the previous one finish.

So - I want to add some smarts to the script that if it see's a particular process running (tape.sh) that it will exit the script without doing anything further. If process isnt found it continues on with rest of script. Please assist.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2013
Top Expert 2013
Commented:
Add this near the top of the script:

if ps -ef | grep -q tape.s[h]; then exit; fi

Please note the square brackets, they're important!

Author

Commented:
Ok I will test this. What does the bracket around the h do?
Most Valuable Expert 2013
Top Expert 2013

Commented:
When grep'ing the ps list the grep command and its argument become part of that list. So grep will find itself running and always return a  "success" return code. If we use regular expression syntax in the argument (the brackets constitute a so-called character class) then the argument and what grep is looking for will be different- grep will no longer find itself running, returning "success" only if the argument (with the brackets stripped) is found.

Another often used method is adding "| grep -v grep", but the above way is shorter and (in my eyes) more elegant :-)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

run your process through flock. this is a general good practice to avoid running multiple times anything that runs in a crontab.
or you can make it a daemon ( or pseudo-daemon in 2 lines of shell ) and sleep a configurable amount of time between runs.

i'm using the linux syntax for flock

ex with flock:
* * * 10,12,14,16,18 0 flock -n /var/run/mycron_is_running.lck sh mycron.sh

ex as pseudo-daemon ( i'm also using flock so it respawns if it crashes or is killed )
* * * * * flock -w 60 /var/run/mycron_pseudo_daemon_running.lck sh -c 'while sleep 3600 ; sh mycron.sh ; done'

you can make it as complex as needed such as run it once every hour exactly if it took less than 50 minutes to run and wait 10 minutes otherwise or something similar

Author

Commented:
Thanks. This works perfectly.

Author

Commented:
Thanks Skullnobrains for the comment. I will keep flock in mind for future use. In this case multiple users use this script | some at same time. So just adding a variable in grep to search for same user running cronjob did the trick. Not sure Flock would work in this case.
it would allow it to run once at a time with whatever number of users.
fuser would let you know who locked the file should that be necessary.

if you wanted multiple users to be able to launch it once each, you'd need to build the name of the lock file with the user's names or ids so each user has it's own lock file.

btw if pgrep is available on your system, "pgrep tape.sh" is a nice but less portable alternative

but as long as you have a working solution... see ya around

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial