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.
k3vsmithAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

woolmilkporcCommented:
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!
0

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?
0
woolmilkporcCommented:
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 :-)
0
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

skullnobrainsCommented:
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
0
k3vsmithAuthor Commented:
Thanks. This works perfectly.
0
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.
0
skullnobrainsCommented:
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
0
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
Linux

From novice to tech pro — start learning today.