检查程序是否正在运行,如果不在 perl 中运行它 [英] check if program is running and run it if not in perl

查看:60
本文介绍了检查程序是否正在运行,如果不在 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");

这篇关于检查程序是否正在运行,如果不在 perl 中运行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆