JP Vossen on 23 Sep 2009 15:06:57 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] correct way to do this in bash


> Date: Wed, 23 Sep 2009 06:46:04 -0400
> From: Mag Gam <magawake@gmail.com>
> 
> What is the best way to kill and restart a process even if using cron?
> 
> kill `ps -ef | grep "process name" | awk '{print $2}' ` ?

Ouch!  If you want painful, use: kill `pgrep "process name"`
But really, keep reading and use pkill.


> is there something a bit more cleaner? I am hesitant to parse the output of ps

I agree with Sean that the "right" way is for 'process' to write its PID 
to a run file and use that.  But if it doesn't and you can't modify it, 
that's out.  You could write a trivial shell wrapper around 'process' to 
fix that; lots of /etc/init.d "rc" files do that.  But that's starting 
to get into overkill and too many moving parts.

The easy way is to use pkill (as Sean noted), or pgrep (same thing), or 
killall (as I noted in my sample script).

'man' any/all of those for the details, pkill is probably the easiest.


While my previous script was untested, I'm pretty sure it's at least 99% 
of what you need.  You should be able to use the 'sleep' command in 
place of 'process' to test it out.  E.g.:
	$ sleep 15 &
	[1] 9412

	$ pgrep -l sleep
	9412 sleep

	$ pkill sleep
	[1]+  Terminated              sleep 15

So you edit my script and replace 'process' with sleep, play around, 
then edit it back when done.  I'd thought about using a variable for the 
process/program to work on instead of hard-coding it, I guess I should 
have...


Otherwise, I like Mark's FIFO idea too.  It has more moving parts, but 
would give you 100% uptime I think.

Good luck,
JP
----------------------------|:::======|-------------------------------
JP Vossen, CISSP            |:::======|      http://bashcookbook.com/
My Account, My Opinions     |=========|      http://www.jpsdomain.org/
----------------------------|=========|-------------------------------
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug