in reply to Start a service, wait 10 seconds, and stop the service.

Thanks, to everyone who answered. I am still not sure what I am going to do. For now I am using this code below. However, I am going to try to do as davidrw suggested, as this seems the simplest. I suppose I will have to be calling net start from within the script using backticks or system or something, but that doesn't seem like it should be a problem.
use strict; use warnings; use IPC::Open3; #my $result = system("C:\\Programme\\Tor\\tor.exe &"); #print "sleeping"; #sleep(3); my ($WR1, $RD1, $ER1); my $tor_fhs = {}; my $pid = start_tor($tor_fhs); sleep 1; stop_tor($tor_fhs); sub start_tor { my $tor_fhs = shift; open3( $tor_fhs->{wr}, $tor_fhs->{rd}, $tor_fhs->{err}, "C:\\Progr +amme\\Tor\\tor.exe" ); print "tor started\n"; return $pid; } sub stop_tor { my $tor_fhs = shift; close($tor_fhs->{wr}); local($/) = undef; close($tor_fhs->{rd}); #close($ER1); print "tor stopped\n"; }
UPDATE: Ack, no, maybe that wasn't such a good idea. This starts the service, but doesn't stop it, so after a bit of playing around I had a bunch of processes that I had to stop using ctrl-alt-delete. Maybe time to stop monkeying with things I don't really understand and get this working using net start and net stop.

Replies are listed 'Best First'.
Re^2: Start a service, wait 10 seconds, and stop the service.
by cool_jr256 (Acolyte) on Jun 17, 2005 at 17:20 UTC
    You're missing the
    waitpid $pid, 0;
    That's why your processes don't get stopped properly..... Update:
    Tried it without the 'waitpid' and exactly as predicted left me with "ghost" processes which I had to kill manually...
Re^2: Start a service, wait 10 seconds, and stop the service.
by ikegami (Patriarch) on Jun 17, 2005 at 17:38 UTC

    You never kill the process, so it never dies.

    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";