2012-06-30 275 views

回答

7

發送一個0(零)信號到您要使用kill函數檢查的進程ID。如果該過程存在,則該函數返回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"); 
+1

好的,但如何檢查進程是否正在運行他的名字? :D – Luke

+1

http://search.cpan.org/~durist/Proc-ProcessTable-0.39/ProcessTable.pm我對perl沒有太多的瞭解,但是我確信這就是你要找的東西。 – Sturm

+3

另請參閱http://stackoverflow.com/questions/3844168/how-can-i-check-if-a-unix-process-is-running-in-perl – Bruce

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 
+0

此答案僅適用於* nix。在大多數情況下,使用內置命令和CPAN模塊的要點之一是不必擔心可移植性。 'kill 0,$ pid'選項可用於其他操作系統,如Windows。 :-) –

+0

我想你可以使用'pgrep',grepping正在運行的進程很常見,有一個特殊的命令;) –

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; 
3

我想「殺0 ......」的事情,但如果你沒有權限進程,那麼這將不起作用,因爲「kill 0」只檢查是否可能發送信號。由於你沒有權限(你的UID不是0,進程也不是UID),你總是會得到錯誤的。

我也嘗試去Linux中的帳戶/ proc /來檢查PID目錄是否存在,但該部分不是非常便攜的:對linux很好,但是在沒有額外的愛。

所以我寫了這樣子,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"; 
} 

玩得開心! :)

1

如果$pid爲空,則進程沒有運行:

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