Perl (SSH for the remote host, issues a command and closes the session without waiting for its completion).

Script goes to the remote server and launches the shell script "snap.sh" using Net :: SSH :: Perl. This shell script takes almost 10 minutes, and my perl program waits until it exits. I want to run a shell script on a remote server, and the program must close the SSH session without waiting for the script to finish on the remote server.

my $ssh = Net::SSH::Perl->new($host, protocol =>2); $ssh->login($username, $password); my $cmd="./bin/snap.sh"; my($stdout, $stderr, $exit) = $ssh->cmd($cmd); 
+4
source share
4 answers

Unconfirmed, but can you do what ssh -f does?

 my $ssh = Net::SSH::Perl->new($host, protocol =>2); $ssh->login($username, $password); defined (my $pid = fork) or die "fork: $!"; if ($pid) { close $ssh->sock; undef $ssh; } else { my $cmd="./bin/snap.sh"; my($stdout, $stderr, $exit) = $ssh->cmd($cmd); POSIX::_exit($exit); } 
+1
source

Find the nohup command. Here is a quick post to get you started . For completeness, this is what should work in your case ...

 my $cmd="nohup ./bin/snap.sh &"; 
+4
source

To run a background job on a remote host, you also need to separate tty from any control on the local computer. Try the command, for example:

 my $cmd = "./bin/snap.sh < /dev/null > /dev/null 2>&1 &"; 

I think using nohup is optional.

+1
source

I am using Net :: OpenSSH for this. It has a spawn method that does exactly what you are looking for.

  my %conn = map { $_ => Net::OpenSSH->new($_) } @hosts; my @pid; for my $host (@hosts) { open my($fh), '>', "/tmp/out-$host.txt" or die "unable to create file: $!"; push @pid, $conn{$host}->spawn({stdout_fh => $fh}, $cmd); } waitpid($_, 0) for @pid; 
+1
source

Source: https://habr.com/ru/post/1337330/


All Articles