Operating System - HP-UX
1833042 Members
2417 Online
110049 Solutions
New Discussion

Re: set a time-limit on child processes?

 
Trever Furnish
Regular Advisor

set a time-limit on child processes?

Is this possible in the posix shell or do I need to use a different language?

I have a shell script that runs a bunch of other scripts ("dumpers"), one after another, collecting their output. In order to scale the process up I'd like to start running a certain number of dumpers simultaneously and would also like to limit the time they have to run.

For example, I'd like to spawn at most X dumpers, then wait for one to complete and spawn another to replace it, proceeding until the entire list of dumpers has been executed, but I need to be able to enforce a time limit to make sure I don't wait indefinitely because of one misbehaving dumper.

The sh-posix man page says the only argument for the wait command is a PID.

If I were doing this in a different language (perl?), my parent process would fork off children and loop continuously, killing children that live too long - is there a way to do something like that in sh?
Hockey PUX?
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: set a time-limit on child processes?

Hi Trevor:

The concept you cite can work in a shell too. Launch your children as background processess. As soon as you have, capture their 'pid':

# ./process1 &
# mychild1=$!
# ./process2 &
# mychild2=$!

Now have your parent wait as long as it wants. If a child hasn't finished in time, issue a 'kill ${mychild}' as appropriate.

Regards!

...JRF...
Trever Furnish
Regular Advisor

Re: set a time-limit on child processes?

Thanks, James, but I'm confused - if I'm waiting, how can I issue a kill? The kill doesn't get an opportunity to run until the wait completes.

That sounds wrong even as I type it, but what am I missing?
Hockey PUX?
Trever Furnish
Regular Advisor

Re: set a time-limit on child processes?

D'OH!!!

Post again, James, and I'll give you a bunny. I didn't realize you never said "wait" until I read the message again after my reply. :-)

Gonna be one of those days...
Hockey PUX?
James R. Ferguson
Acclaimed Contributor

Re: set a time-limit on child processes?

Hi Trever:

You can have a loop in the parent that sleeps, periodically wakes up, and interrogates the various child processes running in the background to see if they are alive. A 'kill -o ' is used to test for the validity of a pid and hence the continued running of the process. When you find that all of your children are dead, you can go away yourself. If you conclude that someone has lived too long, you can terminate him/her.

Regards!

...JRF...