Results 1 to 5 of 5

Thread: Linux Kill script

  1. #1
    Senior Member
    Join Date
    Feb 2003
    Location
    Memphis, TN
    Posts
    3,747

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

  2. #2
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915
    Hey Hey,

    Code:
    #!/bin/sh
    
    export procID=`cat /path/to/file.pid`
    kill -9 $procID
    Note that those are backticks -- ~ key but that should do the trick for you... it's simple and to the point..

    I didn't use a process ID or kill but here's the logic

    Peace,
    HT

  3. #3
    Senior Member
    Join Date
    Feb 2003
    Location
    Memphis, TN
    Posts
    3,747
    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.
    =

  4. #4
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    One-liner:

    kill `cat /var/run/file.pid`
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  5. #5
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    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 03: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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •