use strict; use warnings; sub start { my $pid; { # We don't need to talk to the process, # So we don't need open3. # Close communication channels. local *STDIN; local *STDOUT; local *STDERR; # Launch child process. $pid = eval { system 1, @_ }; # 1 == P_NOWAIT } die("Can't spawn-NOWAIT: $!\n") if !$pid || $pid < 0; return $pid; } sub stop { my ($pid) = @_; kill(15, $pid) and waitpid($pid, 0); } # Arguments to the program must be passed in seperate function args # For example, # # my $tor_pid = start('C:\\Programme\\service.exe', 'arg1', 'arg2'); # # If you don't, you'll get the PID of the shell that's created # to launch the program, and stop() won't work. my $tor_pid = start('C:\\Programme\\service.exe'); print "tor started\n"; sleep 3; stop($tor_pid); print "tor stopped\n";