November 23rd, 2006 07:01 PM
Linux Kill script
I'm trying to write a script to pull a PID number out of a file and then pipe that into the kill command.
For example..I wrote a start script to start a daemon, but I need to write a stop script as well to stop the daemon....when the daemon is started it creates a file "file.pid" which holds the process number that the daemon started with. I need to pull that number from the .pid file and feed it to the kill command.
I'm not the best programmer in the world, but if somebody can give me the syntax or what not for that, i can probably take it from there...or if someone has a better idea I'm open to that as well....I tried killall, but no joy.
November 24th, 2006 12:51 AM
Note that those are backticks -- ~ key but that should do the trick for you... it's simple and to the point..
export procID=`cat /path/to/file.pid`
kill -9 $procID
I didn't use a process ID or kill but here's the logic
IT Blog: .:Computer Defense:.
(Pronounced Pinched): Acronym - Point 'n Click Hacked. As in: "That website was pinched" or "The skiddie pinched my computer because I forgot to patch".
November 24th, 2006 04:26 AM
Thank you very much...I'm not much of a programmer...more of a networking type of guy, but I know enough where if I see some samples I can put something together.
Thanks again HT.
November 24th, 2006 10:12 AM
kill `cat /var/run/file.pid`
Experience is something you don't get until just after you need it.
November 24th, 2006 02:42 PM
Would recommend you use -15 instead of -9 first. -15 is a normal close process signal. Some processes are particularly ticky about having everything dropped out beneath them (which is what 9 does...think of it as 'die, die now, don't do anything else but die'). Kill -9 is great when a process is truly hung, but if you are just wanting to stop a running process that isn't hung, 15 is much safer.
EDIT: Side note, many variations of unix also have a pkill command, which will let you kill by the process name, rather than the PID, which could also be helpful for scripting a shutdown process.
EDIT 2: 'kill' is also a misnomer. Kill is really a signaling program that can be used to send out of program signals to a running process. Somewhere in your unix source includes should be a signal.h if you want to look at all the different signals you can send. AFAIK, you'll need to send what signal you want to send to the process when you issue the command (so kill `cat /somedir/someprocess.pid` should also have the -15 or -9 (or whichever) in there as well.
Last edited by nebulus200; November 24th, 2006 at 02:46 PM.
There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.
(Merovingian - Matrix Reloaded)