检查程序是否正在运行,如果不在perl中则运行它

时间:2012-06-30 11:15:49

标签: perl

我想知道如何检查程序是否正在运行并运行此程序,如果没有。

5 个答案:

答案 0 :(得分:8)

使用kill函数向要检查的进程ID发送0(零)信号。如果进程存在,则函数返回true,否则返回false。

示例:

#-- check if process 1525 is running
$exists = kill 0, 1525;
print "Process is running\n" if ( $exists );

您可以使用系统调用从命令行调用任何程序。这仅在您不需要捕获程序输出时才有用。

#!/usr/bin/perl
use strict;
use warnings;
my $status = system("vi fred.txt");

或者如果您不想涉及shell:

#!/usr/bin/perl
use strict;
use warnings;
my $status = system("vi", "fred.txt");

答案 1 :(得分:4)

与其他答案类似,但您需要确保并使用“grep -v grep”与grep调用本身不匹配。这将确保您在不想要的时候不会评估为真。

use strict;
use warnings;

my($cmd, $process_name) = ("command here", "process name here");

if(`ps -aef | grep -v grep $process_name`) {
    print "Process is running!\n";
}#if
else {
    `$cmd &`;
}#else

答案 2 :(得分:3)

我试过"杀死0 ......"但是,如果你没有这个过程的许可,那就不会起作用,因为" kill 0"仅检查是否可能发送信号。由于您没有权限(您的UID不为0且进程也不是UID),因此您将始终为假。

我还尝试在Linux中使用帐户/ proc /来检查PID目录是否存在,但该部分不是非常便携:对Linux有好处,但在UNIX中没有真正在其他地方工作过额外的爱。

所以我写了这个子,HTH:

sub is_running() {
    my $pid = shift;
    my @proc_data = split(/\s+:\s+/, 
                          `ps uax | awk '{print \$1,":",\$2}' | grep $pid`);
    return (@proc_data && $proc_data[1] == $pid) ? $proc_data[0] : undef;
}

用法很简单,非常有用,因为它还会返回您的进程所有者:

my $pid = 12345;
my $owner = &is_running($pid);
if ($owner) {
    print "Process with PID $pid is running and owned by \"$owner\".\n";
}

玩得开心! :)

答案 3 :(得分:1)

如果$pid为空,则表示进程未运行:

my $pid = `ps -C $progname -o pid=`; # non-windows solution, sorry

答案 4 :(得分:0)

我们根据Linux上守护程序启动pid-file的内容,使用它来检查守护程序是否正在运行:

#!/usr/local/bin/perl
use strict;
use warnings;

use feature qw/ say /;

# vars we report
my (
    $token,         # optional arg - check against cmd_line if specified
    $pid_file,      # daemon pid-file, e.g. /var/run/mysqld/mysqld.pid
    $pid,           # the pid to investigate...
    $pid_running,   # found a pid and it is running
    $cmd_line,      # cmd_line of the running pid
    $result,        # 0-OK, 1=FAIL, exit value
    $error,         # error message if necessary
  );

# working vars
my ( $fh, $cmd_file );

die "Daemon pid-file required" unless scalar @ARGV >= 1;
( $pid_file, $token ) = @ARGV;

if ( -s $pid_file ) {
  open( $fh, "<", $pid_file ) or die "open $pid_file: $!";
  ( $pid ) = <$fh>; chomp $pid; close $fh;

  $pid_running = kill 0, $pid;

  if ( $pid_running ) {
    $cmd_file = "/proc/$pid/cmdline";
    local($/) = "\0"; # C-String NULL terminated
    open( $fh, "<", $cmd_file ) or die "open $cmd_file: $!";
    ( $cmd_line ) = <$fh>; close $fh;
    if ( $cmd_line && $token && $cmd_line !~ m/$token/ ) {
      $error = "token not found: $token in $cmd_line";
    }
  }
  else {
    $error = "process not found: $pid";
  }
}
else {
  $error = "file not found: $pid_file";
}

# if TOKEN - OK running process with matching cmdline
$result = $token ? ( $pid_running && $cmd_line && $cmd_line =~ m/$token/ )
                 : ( $pid_running || 0 );

say "token:     ", $token if $token;
say "file:      ", $pid_file;
if ( $pid ) {
  say "pid:       ", $pid;
  say "running:   ", $pid_running;
  say "cmdline:   ", $cmd_line if $cmd_line;
}
say "error:     ", $error if $error;
say "exit:      ", $result ? 'ok' : 'fail';

exit $result;
相关问题