12

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

4

5 回答 5

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");

或者,如果您不想涉及外壳:

#!/usr/bin/perl
use strict;
use warnings;
my $status = system("vi", "fred.txt");
于 2012-06-30T11:19:56.960 回答
5

与另一个答案类似,但您需要确保并使用“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
于 2013-05-03T15:55:54.767 回答
3

我尝试了“kill 0 ...”的东西,但是如果您没有对该进程的权限,那将不起作用,因为“kill 0”仅检查是否可以发送信号。由于您没有权限(您的 UID 不是 0 并且进程也不是 UID),因此您将始终为 false。

我还尝试使用 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";
}

玩得开心!:)

于 2014-05-22T13:22:41.627 回答
2

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

my $pid = `ps -C $progname -o pid=`; # non-windows solution, sorry
于 2015-03-20T20:02:15.147 回答
0

我们使用它来检查守护进程是否正在运行,基于 Linux 上守护进程启动 pid 文件的内容:

#!/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;
于 2014-01-15T19:48:29.840 回答