Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout for a cmd command?

Tags:

cmd

perl

i just have the following command in cmd

perl csv2rrd3.pl

but it always run for a long time. Is there any way to set a timeout for it? or i should do it in the perl script?? And how?

Thanks

like image 862
Danny Ho Avatar asked Dec 08 '22 19:12

Danny Ho


2 Answers

Inside of Perl, you can set an alarm to go off after a few seconds. You can then use a signal handler to catch the event. Only one alarm may be active at any time

{ # enter new scope
    # set signal handler
    local $SIG{ALRM} = sub {
       # do cleanup, like closing sockets or whatever
       print STDERR "exited with ALARM\n";
       exit;
    };
    alarm 30; # try half a minute
    ...; # do expensive stuff
}
like image 70
amon Avatar answered Dec 11 '22 09:12

amon


From the windows command line, you can first start the perl script in its own window, chain it with the timeout command to wait x seconds (here 2) and then chain it again with the taskkill command to kill the process.

start perl csv2rrd3.pl & timeout -t 2 & taskkill /IM perl.exe

The line above would kill every perl process. To kill only your just started one, you could use a custom window title.

start "KILLME" perl csv2rrd3.pl & timeout -t 2 & taskkill /FI "WINDOWTITLE eq KILLME" /IM perl.exe
like image 35
Matthias Avatar answered Dec 11 '22 08:12

Matthias