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 (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.
LinuxShell Scripting

Avatar of undefined
Last Comment

8/22/2022 - Mon

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

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 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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

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 tape.sh" is a nice but less portable alternative

but as long as you have a working solution... see ya around
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.