|
Home > Archive > Unix questions > February 2006 > How to check the same process running twice
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to check the same process running twice
|
|
| suresh 2006-02-05, 2:47 am |
| I am planning to run a script on crontab. The crontab will trigger the
script every 5 minutes once. When the script is triggered via crontab,
the script should check whether a previous instance of it the same
script is still running. If so then the current trigger should not
happen.
In other words, the script should have check_process function, which
every time should check whether the same process is running more than
once.
How to do this above logic.
Suresh Ramamurthy
| |
| Bit Twister 2006-02-05, 7:47 am |
| On 4 Feb 2006 23:03:09 -0800, suresh wrote:
> I am planning to run a script on crontab. The crontab will trigger the
> script every 5 minutes once. When the script is triggered via crontab,
> the script should check whether a previous instance of it the same
> script is still running. If so then the current trigger should not
> happen.
If no lock file
touch /a/lock/file
script_2_run
rm /a/lock/file
or if results of checking ps/grep is negative, run script.
For extra points,
Depending on distribution
man ps
man grep
man pgrep
Depending on shell
! bash script introduction documentation
http://tldp.org/LDP/intro-linux/html/index.html
! bash script advanced documentation
http://tldp.org/LDP/abs/html/index.html
| |
| martin.witte@gmail.com 2006-02-05, 7:47 am |
| Does this help?
I have two bash shells running:
martin@jordaan:~$ ps -ef|grep bash|grep -v grep|wc -l
2
martin@jordaan:~$
I remove the 'grep bash', if this command returns more than 1 you have
more instances running
| |
| Jeremiah DeWitt Weiner 2006-02-06, 5:56 pm |
| martin.witte@gmail.com <martin.witte@gmail.com> wrote:
> I have two bash shells running:
> martin@jordaan:~$ ps -ef|grep bash|grep -v grep|wc -l
> 2
First issue: you should never have to use 'grep | grep -v'. You
can accomplish the same thing as the above by doing
ps -ef | grep -c '[b]ash'
Second and more important issue: what if someone is running 'man
bash' or 'vi .bashrc' or 'file /usr/share/zoneinfo/Africa/Lubumbashi',
or any number of other commands that happen to have the string "bash" in
them? Grepping the output of ps is not a very reliable way to tell if a
process is running.
--
Oh to have a lodge in some vast wilderness. Where rumors of oppression
and deceit, of unsuccessful and successful wars may never reach me
anymore.
-- William Cowper
| |
| Conrad J. Sabatier 2006-02-06, 8:48 pm |
| In article <ds89cn$3pu$3@reader2.panix.com>,
Jeremiah DeWitt Weiner <jdw@panix.com> wrote:
>
>
>martin.witte@gmail.com <martin.witte@gmail.com> wrote:
>
> First issue: you should never have to use 'grep | grep -v'. You
>can accomplish the same thing as the above by doing
>ps -ef | grep -c '[b]ash'
>
> Second and more important issue: what if someone is running 'man
>bash' or 'vi .bashrc' or 'file /usr/share/zoneinfo/Africa/Lubumbashi',
>or any number of other commands that happen to have the string "bash" in
>them? Grepping the output of ps is not a very reliable way to tell if a
>process is running.
True. What I like to do in my scripts is have the script write its pid to a
file (after first checking to see if such a file already exists of course).
If the file already exists, then "if ps -p $(cat pid-file) 2>/dev/null" is a
pretty surefire way of catching an already running instance.
The script deletes the pid-file on exit, of course.
--
Conrad J. Sabatier <conrads@cox.net> -- "In Unix veritas"
| |
| Jeremiah DeWitt Weiner 2006-02-07, 6:03 pm |
| Conrad J. Sabatier <conrads@serene.no-ip.org> wrote:
> True. What I like to do in my scripts is have the script write its pid to a
> file (after first checking to see if such a file already exists of course).
> If the file already exists, then "if ps -p $(cat pid-file) 2>/dev/null" is a
> pretty surefire way of catching an already running instance.
Yup. Just to be extra-safe, I like to confirm, as far as possible,
that the process which has that PID is in fact an instance of the script
I'm interested in. It's possible that the PID could have gotten reused
by an unrelated process, albeit unlikely.
> The script deletes the pid-file on exit, of course.
And remember to trap signals that would cause your program to exit!
And teach other admins not to use "kill -9" all the time... *grumble*
--
Oh to have a lodge in some vast wilderness. Where rumors of oppression
and deceit, of unsuccessful and successful wars may never reach me
anymore.
-- William Cowper
| |
|
| Jeremiah DeWitt Weiner wrote:
> martin.witte@gmail.com <martin.witte@gmail.com> wrote:
>
> First issue: you should never have to use 'grep | grep -v'. You
> can accomplish the same thing as the above by doing
> ps -ef | grep -c '[b]ash'
Without -f, you don't need the grep mangling:
ps -e | grep -c bash
(Linux' ps syntax above - the equivalent that works on my BSD system
would be ps -Ac).
>
> Second and more important issue: what if someone is running 'man
> bash' or 'vi .bashrc' or 'file /usr/share/zoneinfo/Africa/Lubumbashi',
> or any number of other commands that happen to have the string "bash" in
> them? Grepping the output of ps is not a very reliable way to tell if a
> process is running.
Also addressed above, since those options to ps suppress printing of
command arguments. But the script name still might be confused with
another process name; other posters' pidfile ideas are probably
superior.
>
> --
> Oh to have a lodge in some vast wilderness. Where rumors of oppression
> and deceit, of unsuccessful and successful wars may never reach me
> anymore.
> -- William Cowper
|
|
|
|
|