[Solved] open and close perl processes with time out cross-platform


You can use the operating system to signal an ALRM (alarm), which you can then exit your process on after a set number of seconds.

So for instance, if at the top of your script you put:

alarm 300;

This will cause your process to receive an ALRM signal. This will kill your process immediately, or you can trap it and clean up your process before it dies.

For further reading: http://perldoc.perl.org/functions/alarm.html

You can trap the signal like this:

$SIG{ALRM} = sub {

# Clean up process here and exit

};

In response to your edit “edit: how do I get the process ID for the started process on windows and unix and how do I kill it at the timeout”

The process ID is stored in perl as $$, so:

print 'My process ID is:', $$, "\n";

3

solved open and close perl processes with time out cross-platform