1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
sub StartEditor() { eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 60; #start the Editor exe my $commandline='"Editor.exe" /CAS-FILE=$file'; if ( (my $retcode = system($commandline)) != 0) { print (STDERR "'$commandline' returned non-ok confirmation code '$retcode'\n"); } alarm 0; }; if ($@) { # timed out print "StartEditor(): TIME OUT!\n"; #kill Editor.exe system ('taskkill /F /IM "Editor.exe"'); die #unless $@ eq "alarm\n"; } else { # didn't timeout print "StartEditor(): Finished Editor.exe\n"; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#!/usr/bin/perl use strict; use warnings; my $commandline='"Editor.exe" /CAS-FILE=$file'; my $timeout=160; #Sekunden local $SIG{CHLD}='IGNORE'; my $pid=fork; die("Fork failed!") unless(defined($pid)); if($pid) { my $start=time(); while($start+$timeout > time()) { sleep 1; last unless(kill(0,$pid)); } # Zeit abgelaufen und Prozess läuft noch if($start+$timeout <= time() && kill(0,$pid)) { # Prozess beenden # erster Versuch # freundlich fragen kill(15,$pid); sleep 2; # zweiter versuch if(kill(0,$pid)) { kill(15,$pid); sleep 2; # das dritte mal Prozess abwürgen! kill(9,$pid) if(kill(0,$pid)); } } } else { # externes Programm mit der aktuellen Prozess ID starten exec($commandline); }
1
2
3
4
5
for /F %%i in ('tasklist /fi "IMAGENAME eq prog.exe"') do set r=%%i
if /i "%r%"=="prog.exe" goto progruns
...
:progruns
taskkill /f /fi "IMAGENAME eq prog.exe"
1 2 3 4
#kill Editor.exe system ('taskkill /F /IM "Editor.exe"'); die #unless $@ eq "alarm\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use Win32; use Win32::Process; my $p; my $commandline='"Editor.exe" /CAS-FILE=FILENAME'; my $dir = $dir_exe.'\\CDB Editor.exe'; print "Starting CDB Editor.exe ... \n"; Win32::Process::Create( $p, $dir, $commandline, 1, NORMAL_PRIORITY_CLASS, '.', ) or die Win32::FormatMessage( Win32::GetLastError() ); print "Waiting 15 seconds before killing $commandline\n"; for (1 .. 15) { print; print "WAITING...\n" ; sleep 1; } $p->Kill(0) or die "Cannot kill '$p'";