Perl脚本不是杀死tcl进程

时间:2015-12-01 19:12:34

标签: perl solaris

我有一个perl脚本。它并没有杀死我的tcl进程。以下是我的流程:

UID 14439 10897 0 13:55:44 pts / 26 0:00 / prod / bin / wish / prod / bin / q

这是我的perl脚本:

#!/usr/bin/env perl
#
# Simple kill script that allows users to kill specific processes
# using sudo

use BPub::Platform qw(solaris);

# Assume a taint mode type environment
$ENV{PATH} = '/opt/csw/bin:/usr/bin:/bin';

use strict;
use Getopt::Long;

if (!@ARGV) {
    print "Usage: bkill [-9] pid <pid...>\n";
    exit 1;
}

my $dashnine;
GetOptions('9' => \$dashnine);

# If the process name isn't in this hash, then you don't get to kill
# it
my %allowed_process_names = (
    'dtsession'  => 1,
    'wish'          => 1,
    'compose'    => 1,
);

# True if the given process name is one that we can kill
sub allowed_to_kill {
    return $allowed_process_names{$_[0]};
}

# Takes a pid and returns the process name
sub process_name {
    my ($pid) = @_;


    my $out = `/usr/ucb/ps -c $pid | /bin/grep $pid | /bin/cut -b 25-1000`;
    if ($?) {
        print "Error running ps command: $!\n";
        exit 2;
    }

    chomp($out);

    return $out;
}

foreach my $pid (@ARGV) {

    # tainted, remember?
    if ($pid =~ /^(\d+)$/) {
        my $safe_pid = $1;

        my $name = process_name($safe_pid);

        if (allowed_to_kill($name)) {
            my @cmd = "/usr/bin/kill";
            push (@cmd, '-9') if $dashnine;
            push @cmd, $pid;

            print "@cmd\n";
            print "Killing $name ($pid)\n";
            system(@cmd);
        } else {
            print "Not allowed to kill $safe_pid: $name.\n";
        }
    } else {
        print "Invalid pid: must be a number: $pid\n";
    }
}

当我使用sudo bkill PID运行脚本时。我收到一条错误消息:

bpub-xserver7-prod^UID118-> sudo bkill 14439
Password:
Not allowed to kill 14439: wish8.0.

在这个剧本中有什么我可以更好的吗?我怎么能解决这个问题,我摆脱了tcl进程。

2 个答案:

答案 0 :(得分:1)

您的错误消息正在发出的程序名称是wish8.0。因此,最简单的修复(最严格的)是将'wish8.0' => 1添加到%allowed_process_names

或者您可以通过执行以下操作来降低搜索范围:

my @allowed_process_names = qw(dtsession wish compose)
sub allowed_to_kill {
    return scalar grep { index($_[0], $_) == 0 } @allowed_process_names
}

将找到以任何允许的进程名称开头的任何字符串。 (更改为&gt; = 0或!= -1以允许它在任何地方匹配)

您也可以使用正则表达式。以下将匹配以提供的程序名称开头的任何程序。如果删除前导插入符号,它将匹配它们在字符串中的任何位置。

my $allowed_process_names = qr/^(?:dtsession|wish|compose)/;
sub allowed_to_kill {
    return $_[0] =~ /$allowed_process_names/;
}

或者如果您想以编程方式构建正则表达式:

my @allowed_process_names = qw(dtsession wish compose)
my $allowed_process_names = join('|', map quotemeta, @allowed_process_names);
$allowed_process_names = qr/^(?:$allowed_process_names)/;

答案 1 :(得分:0)

# If the process name isn't in this hash, then you don't get to kill
# it
my %allowed_process_names = (
    'dtsession'  => 1,
    'wish'          => 1,
    'compose'    => 1,
);
...
    my $out = `/usr/ucb/ps -c $pid | /bin/grep $pid | /bin/cut -b 25-1000`;
...
Not allowed to kill 14439: wish8.0.

您的ps管道正在将流程标识为wish8.0,而不是wish/prod/bin/wish。您需要为&#34; wish8.0&#34;添加一个条目。到此%allowed_process_names哈希,以便使用此perl脚本来终止此过程。

如果您要查看/prod/bin/wish,您可能会发现它是指向wish8.0文件的符号链接。