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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
k3vsmithAuthor Commented:
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 :-)
Get Blueprints for Increased Customer Retention

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

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

but as long as you have a working solution... see ya around
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.