Why is "kill" different from terminal and system ()?
When I run below script that will execute sleep 123
on the remote host and then kill the local ssh process that will exit sleep 123
on the remote host. This is expected behavior.
However, if I uninstall system("kill $p")
and execute the command kill
from terminal with process id, local ssh is killed and remote sleep 123
persists. Not expected.
# /usr/gnu/bin/kill 961
Killed by signal 15.
Question
Why does the same command kill
differ depending on its execution from Perl system()
and from terminal, and how sleep 123
does the remote command survive when the ssh connection is killed?
#!/usr/bin/perl
use strict;
use warnings;
use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new(5);
my $pid = $pm->start;
my $p = $pid;
if (!$pid) {
system("ssh 10.10.47.47 sleep 123");
$pm->finish;
}
$p = qx(/usr/bin/pgrep -P $p);
print "ssh pid is $p\n";
system("/usr/gnu/bin/kill $p");
source to share
Even for localhost, ssh
it will take a few milliseconds to connect and execute the command.
When killed by a script, it ssh
dies within a few milliseconds of startup, so there will never be a chance to execute the command on the remote host.
When a person kills a person, ssh
has a few seconds to connect and execute.
In other words, it sleep
doesn't die in the first case - it never starts.
You can confirm this by making your script wait a second before killing ssh.
source to share