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

mbzjlibv  于 2023-03-13  发布在  Perl
关注(0)|答案(6)|浏览(157)

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

nhn9ugyo

nhn9ugyo1#

向要使用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");
0s7z1bwu

0s7z1bwu2#

类似于另一个答案,但是您需要确保并使用“grep -v grep”来不匹配grep调用本身,这将确保在您不希望的时候不会计算为true。

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
bf1o4zei

bf1o4zei3#

我试过“kill 0...”,但是如果你没有进程的权限,那就不起作用了,因为“kill 0”只检查是否可以发送信号。因为你没有权限(你的UID不是0,进程也不是UID),你总是得到false。
我还尝试使用Linux中的帐户/proc/来检查PID目录是否存在,但这部分的可移植性不是很好:这对Linux很好,但如果没有额外的爱好,就不能在UNIX的其他地方真正工作。
所以我写了这篇文章:

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

玩得开心!:)

bgibtngc

bgibtngc4#

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

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

ut6juiuv5#

我们使用这个来检查守护进程是否正在运行,基于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;
i86rm4rw

i86rm4rw6#

在Linux上,只需检查进程ID是否存在于/proc文件系统中

if (not -d "/proc/$pid") {
  print "Process with $pid is not running");
}

相关问题