Link to home
Start Free TrialLog in
Avatar of k3vsmith

asked on

Check for Linux process in script

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 ( that it will exit the script without doing anything further. If process isnt found it continues on with rest of script. Please assist.
Avatar of woolmilkporc
Flag of Germany image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of k3vsmith


Ok I will test this. What does the bracket around the h do?
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 :-)
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

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 ; 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
Thanks. This works perfectly.
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" is a nice but less portable alternative

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