检查程序是否正在运行,如果不在 perl 中运行它
问题描述:
我想知道如何检查程序是否正在运行,如果没有运行该程序.
I want to know how to check whether is program running and run this program if not.
答
向要使用 kill 函数检查的进程 ID 发送 0(零)信号.如果进程存在,函数返回true,否则返回false.
Send a 0 (zero) signal to the process ID you want to check using the kill function. If the process exists, the function returns true, otherwise it returns false.
示例:
#-- check if process 1525 is running
$exists = kill 0, 1525;
print "Process is running\n" if ( $exists );
您可以使用系统调用从命令行调用任何程序.这仅在您不需要捕获程序的输出时有用.
You can call any program like you would from the command line using a system call. This is only useful if you do not need to capture the output of the program.
#!/usr/bin/perl
use strict;
use warnings;
my $status = system("vi fred.txt");
或者如果您不想涉及 shell:
Or if you don't want to involve the shell:
#!/usr/bin/perl
use strict;
use warnings;
my $status = system("vi", "fred.txt");