in reply to Killing a shell script from within a perl script
You could use open with a pipe in order to get the PID of the child process, and kill it if it doesn't finish in time. Something like this:
#!/usr/bin/perl my $pid = open my $fh, "your_command ... |" or die $!; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 600; # ... optionally read any output from the subprocess # while (<$fh>) { ... } close $fh; # waits for child alarm 0; }; if ($@ eq "alarm\n") { # timed out kill 9, $pid; }
Note that if the shell script itself is running further child processes (not that unlikely...), they would not necessarily be killed, too (depends on the behavior/setup of the shell). In that case you'd have to create a process group and kill the entire group in case of timeout. See Re^2: Killing children's of children for sample code.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Killing a shell script from within a perl script
by strider88 (Initiate) on Mar 04, 2010 at 01:49 UTC |