linux - 在远程主机上使用非返回脚本管理进程

标签 linux perl asynchronous ssh process

我正在使用 ssh 在远程主机上启动脚本。 执行后我需要回到原来的主机。 问题是我要执行的 script1 在远程主机的后台启动了一个工具,直到该进程没有被终止,它才会保留在远程主机上。

ssh -Y -l "test" login 'path/to/script1'
[CTRL+C]

如果我在终端中执行命令,我可以通过键入 CTRL+C 返回 但是现在我想在 Perl 中执行命令,我不能简单地预编译 CRTL+C

system(qq{ssh -Y -l "testlogin" 'path/to/script1'});

现在有人知道如何在不知道 PID 的情况下终止远程主机上的这个进程吗?

最佳答案

我认为您不希望程序等待整个 ssh... 事情完成,而是希望该操作是非阻塞的,以便程序可以启动它并马上去做其他事情。

有多种方法可以做到这一点,具体取决于具体需要什么。我想首先展示规范的 fork+exec 方式。然后我发布了一个小程序,它也使用:system 在后台放置一个作业、一个线程、管道打开和一个模块。

一个基本的方法是fork然后 exec该子进程具有所需的程序。

FORK_EXEC: {
    my @cmd = ('ssh', '-Y', '-l', 'testlogin', 'path/to/script1');
    
    local $SIG{CHLD} = 'IGNORE';  # Don't care about the child process here
    
    my $pid = fork // die "Can't fork: $!";
    
    if ($pid == 0) { 
        exec @cmd;
        die "Shouldn't return after 'exec', there were errors: $!"
    }
}

# the parent (main program) carries on

这假设在任何时候都不需要监视或终止进程。

接下来,这里有一个程序展示了其他几种以非阻塞方式启 Action 业的方法。

对于演示,它会在开始作业后立即返回控制权时打印到屏幕,然后休眠一会儿以便命令(打印 Job done)可以清楚地显示其完成。该程序还经过测试,可以通过 ssh 在远程主机上运行命令。

use warnings;
use strict;
use feature 'say';

# Will use array with command terms when possible, or string if needed
my @cmd = ('perl', '-E', 'sleep 5; say "Job done"');
my $cmd_str = join(' ', @cmd[0,1]) . qq('$cmd[-1]');
say "Command: $cmd_str";

# Command shown in question
#my @cmd = ('ssh', '-Y', '-l', 'testlogin', 'path/to/script1');
#my $cmd_str = join ' ', @cmd;

BACKGROUND_SYSTEM: {  #last;
    # Uses shell. Probably simplest
    system("$cmd_str &") == 0 or die $!;

    say "\nRan in background via system. Sleep 10";
    sleep 10;
};
    
WITH_THREADS: {  #last;
    # Do it any way you want it in a thread
    use threads;

    my $thr = async { system(@cmd) };
    say "\nStarted a thread, id ", $thr->tid, ". Sleep 10";
    sleep 10;

    # At some point "join" the thread (once it completed, or will wait)
    $thr->join;
    # Or, have the thread terminated at program exit if still running
    # $thr->detach;
}

FORK_EXEC: { #last; 
    # Again, have full freedom to do it any way needed    
    local $SIG{CHLD} = 'IGNORE';
    my $pid = fork // die "Can't fork: $!";

    if ($pid == 0) { 
        exec @cmd  or die "Can't be here, there were errors: $!";
    }
    say "\nIn parent, forked $pid and exec-ed the program in it. Sleep 10";
    sleep 10;
};

PIPE_OPEN: {  #last;
    # Strictly no shell. Normally used to read from child as output comes
    my $pid = open(my $pipe, '-|', @cmd)
        // die "cant open pipe: $!";

    say "\nPipe-opened a process $pid.";

    print while <$pipe>;  # demo, not necessary

    close $pipe or die "Error with pipe-open: $!";
};

# Uncomment after installing the module, if desired
#WITH_PROC_BACKGROUND: {  #last;
#    use Proc::Background;
#
#    my $proc = Proc::Background->new( @cmd );
#
#   say "\nStarted a process with Proc::Background, ", $proc->pid, ". Sleep 10";
#    sleep 10;
#}

所有这些都启动一个进程,然后可以自由地继续做其他事情,并打印到屏幕(然后等待进程/线程完成,作为演示)。这些方法具有不同的优点和典型用途。

简要说明

  • system如果它在一个字符串中并且具有 shell 元字符,则将其命令传递给 shell。我们这里执行的是 (&),它告诉 shell 将命令置于“后台”:派生另一个进程并在其中执行命令,从而立即返回控制权。它的工作方式很像我们的 fork+exec 示例,这是 *nix 进行多处理的古老方式

  • 一个单独的thread独立运行,所以我们可以在主程序中进行其他工作。该线程需要在最后进行管理,要么通过 join 对其进行管理,要么通过 detach 对其进行管理,在这种情况下我们不关心它是如何进行的确实(并且它将在程序结束时终止)

  • pipe-open还 fork 了一个单独的进程,从而释放了主程序以继续其他工作。这个想法是文件句柄(我命名为 $pipe)用于接收进程的输出,因为它的 STDOUT 而是 Hook 到该资源(“文件句柄”) .但是,我们不必获取输出,这也可以仅用于产生一个单独的进程。它完全避免了 shell,这通常是一件好事。另见 perlipc

如果您需要实际控制该进程,ssh(在您的主机上)或远程主机上的作业,则需要更多或其他。如果是这种情况,请澄清。


问题最后说

... how to kill this process on the remote host without knowing the PID?

这似乎不同意评论,上面我解决了似乎是任务的问题。但如果确实需要终止远程进程,则从远程主机上的命令打印其 PID——上面显示的所有方法都知道它们启动的进程的 PID——并在脚本中捕获它。然后脚本可以 ssh 并在需要时kill 进程。


另一种不用担心收割子进程的方法是在“双叉”中“否认”它

{
    my $pid = fork // die "Can't fork: $!";
    
    if ($pid == 0) {     
        my $grandkid_pid = fork // die "Cannot fork: $!";

        if ($grandkid_pid == 0) { 
            # do here what you need
            exec @cmd;
            die "Should never get here, there were errors: $!"
        }
    }
    exit;             # parent of second child process exits right away
    waitpid $pid, 0;  # and is reaped
}

# the main program continues

第一个子进程在 fork 后立即退出并被回收,因此 它的 子进程被 init 接管并且有僵尸没有问题。如果需要从外部跟踪或查询该子进程,这不是最佳方式。

关于linux - 在远程主机上使用非返回脚本管理进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67582669/

相关文章:

linux - grep for process id 以获得正确的值

java - bash 命令在 tomcat/java Runtime.getRuntime.exec() 中失败,但可以从命令行运行

regex - SAS 中的正则表达式匹配

java - 如何从客户端角度使用异步方法

java - 如何在 Netty channel 处理程序中安全地执行阻塞操作?

c - 如果无法删除共享内存段会发生什么

linux - 可以将 USB 集线器连接到以太网端口吗?

perl - 如何在 perl 中以秒为单位获取日期差异

perl - 用perl计算特定文件格式的字符串长度

javascript - 从异步或同步 JavaScript 请求返回值