【问题标题】:how to terminate a command that has started in a perl script, using 'system()'如何使用“system()”终止在 perl 脚本中启动的命令
【发布时间】:2021-04-26 09:24:16
【问题描述】:

我已经编写了一个 Perl 脚本来通过脚本运行一些给定的命令。

system("my_command");

运行我的 Perl 脚本后,“my_command”在 Linux 终端上正常启动。后来,我使用 'ctrl+z; 杀死了我的脚本;杀死%%'。但“my_command”仍在运行。我再次尝试了“kill %%”几次,但“my_command”没有终止。 (“my_command”是另一个正常工作的 Perl 脚本)。

我需要的是,如果我终止初始 Perl 脚本/运行程序,那么所有使用 'system()' 启动的命令都应该终止。

有什么办法可以做到吗?

【问题讨论】:

    标签: linux perl


    【解决方案1】:

    system 对此没有太大用处。考虑使用open2,它返回已启动子进程的进程标识符。

    use IPC::Open2;
    
    # A system() like call using open2():
    
    my $pid = open2('>&STDOUT', '<&STDIN', @CommandLine);
    

    您现在可以在$pidkillwaitpid

    这是一个使用一些老式 OOP 的示例,因此当您的程序退出时,您启动的所有进程都将自动成为 killed。我确信有现成的 perl 包以更完整的方式封装了它,但这应该会给你一个大致的想法。

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    package mysystem;
    
    use IPC::Open2;
    
    sub new {
        my $class=shift;
    
        bless {
            'pid' => open2('>&STDOUT', '<&STDIN', @_)
        }, $class;
    }
    
    sub DESTROY {
        my $self = shift;
        $self->kill(15);  # or whatever signal you want to send to it
        $self->wait;
        print "DEBUG PRINTOUT: DONE\n";
    }
    
    sub wait {
        # wait for the process to terminate
        my $self = shift;
        waitpid($self->{pid}, 0);
    }
    
    sub kill {
        # send a signal to the process
        my ($self, $signal) = @_;
        kill($signal, $self->{pid});
    }
    
    sub alive {
        # check if the process is alive
        my $self = shift;
        $self->kill(0) == 1;
    }
    
    sub run {
        # do like system(), start a sub process and wait for it
        my $sys = new(@_);
        $sys->wait;
    }
    
    package main;
    
    sub handler {
        print "Caught signal @_ - exiting\n";
        exit(0);
    }
    
    $SIG{INT} = \&handler;
    
    my $proc = mysystem->new('sleep', '1000');
    print "Pid ". $proc->{pid} . " is " . ($proc->alive()?"alive":"dead") . "\n";
    
    print "Letting the destructor kill it\n";
    

    可能的输出:

    Pid 3833402 is alive
    Letting the destructor kill it
    DEBUG PRINTOUT: DONE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 2015-01-20
      • 2012-10-24
      • 2017-01-13
      • 1970-01-01
      相关资源
      最近更新 更多