September 24th, 2003, 05:36 PM
Killing processes related to a job
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
September 29th, 2003, 11:52 AM
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.
September 30th, 2003, 02:29 PM
I think by 'spawning' he's referring to backticks, execs, syscalls, qx's.. whatever the favourite method is these days..
This is me: http://chris.uk.com
October 1st, 2003, 10:42 AM
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.
December 7th, 2003, 03:15 AM
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
December 9th, 2003, 08:35 AM
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.