#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    In Waterloo, Canada
    Posts
    7
    Rep Power
    0

    Question Killing processes related to a job


    Hi,
    I have a perl script blah.pl that spawns a bunch of other processes,(it runs a bunch of other pl scripts which may do other things, including spawning more child processes) but once in a while I will like to clean the whole damn thing. Basically, do a fresh restart. I am thinking of writing a perl script to kill off all the processes, however I am not sure how to find out which process was actually spawned by the blah.pl I don't want to crash the system or piss off other people, by killing random processes. any suggestions?
    also if you guys know where i can find sample script that will save me the trouble, please let me know. thanks a bunch
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    68
    Rep Power
    12
    >any suggestions?
    When you say spawn I'll assume that you're forking children and exec'ing new programs. Since fork returns the child PID, you can save it in a list and kill all of the processes in the list when you want a clean restart.

    >also if you guys know where i can find sample script that will save me the trouble
    Then you wouldn't have the fun of doing it yourself.
  4. #3
  5. Introspective
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    3,317
    Rep Power
    110
    I think by 'spawning' he's referring to backticks, execs, syscalls, qx's.. whatever the favourite method is these days..

    christo
    This is me: http://chris.uk.com
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    In Waterloo, Canada
    Posts
    7
    Rep Power
    0
    Basically i didn't write the perl scripts my script blah.pl runs, so I don't have access to the child PID, if the scripts do use fork. Also, what christo said is closer to the mark, I believe the scripts use syscalls.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    India
    Posts
    61
    Rep Power
    11
    I think you can try this out

    While running your script just store the Process id of your script

    export PID=`echo $$`

    then after the script is over just grep for the process id of all the processes whose ParentId is equal to PID, and store it in an array. Then again search for the values of these store values in the parentId and store. This is a recursive process. And then you can kill all the processes.

    I haven' t tried writing a script, may be that' s the easiest thing to do here
    Regards
    JK
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    11
    Use a shell with job control and then use the shell's kill command on the job.

    A decent shell will place the job into a separate process group. And it will send a kill to the process group leader using a negative pid in the kill() system call. That will signal all group members simultaneously. Your kernel needs to support broadcast signals for this to work, but all modern kernels do.

IMN logo majestic logo threadwatch logo seochat tools logo