domcyrus has asked for the wisdom of the Perl Monks concerning the following question:

Hi@all! I have some basic questions about perl and what is a good way to handle processes. I want to do an ssh call but I need to contorl the ssh process to know if the process is still running --> status of the process. I also need to be able to kill the process and to set a timeout.
pseudo code #!/usr/local/bin/perl @args = ("ssh", "unaccesible_client", "&"); #bring process in backgrou +nd as we want to continue... #do some stuff $time=start_time(); #after some time get status while ($time > 1000us) { $status=get_status(); if ($status!=running) { $time=1000; } } ... ...

Replies are listed 'Best First'.
Re: process handling
by ptum (Priest) on Nov 06, 2006 at 17:38 UTC

    You may want to look at Net::SSH or even Net::SSH::Perl. Personally, I use this sort of construct, which you might want to plop inside an eval (see alarm):

    use strict; use warnings; use Net::SSH qw(sshopen3); my $command = "/some/remote/command"; my $reader = IO::File->new(); my $writer = IO::File->new(); my $error = IO::File->new(); sshopen3( "$user\@$host", $writer, $reader, $error, $command ) or die +$!;
Re: process handling
by wjw (Priest) on Nov 06, 2006 at 17:41 UTC
    use Net::SSH::Perl; ($out, $err, $exit) = $ssh->cmd($cmd, [ $stdin ])

    If I understand you correctly this should work for you. See This

    ...the majority is always wrong, and always the last to know about it...

Re: process handling
by shmem (Chancellor) on Nov 06, 2006 at 19:17 UTC

    (talking unix/linux) If you want to be able to control a child process, you need it's PID. You get it either with fork:

    my $pid; if (($pid = fork()) == 0) { # child process exec 'some/program','and', 'args'; } else { # parent process $SIG{'CHLD'} = \&sub_that_handles_child_exit_notify; } # dosomething with $pid... my $is_alive = kill 0, $pid; # see if still alive kill 15, $pid; # send a SIGTERM
    or open:
    my $pid = open(FH,"process @args |"); # to read from it my $pid = open(FH,"| process @args"); # to write to it

    For handling input and output, see IPC::Open2. For handling STDERR as well, IPC::Open3. To set timeouts, see alarm.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: process handling
by domcyrus (Acolyte) on Nov 07, 2006 at 09:49 UTC
    Thanks a lot for all of your answers! That's great!!!