如何获取我在 Perl 脚本中触发的 UNIX 命令的进程 ID? [英] How can i get process id of UNIX command i am triggering in a Perl script?

查看:53
本文介绍了如何获取我在 Perl 脚本中触发的 UNIX 命令的进程 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Perl 脚本中触发 UNIX 命令.

I am triggering a UNIX command in Perl script.

我需要 UNIX 命令的进程 ID.

I need the process ID of the UNIX command.

例如,如果我在 UNIX 命令下触发:

For example if i trigger below UNIX command:

# padv -s adv.cfg > adv.out &
[1] 4550

我的进程 ID 是 4550.

My process ID is 4550.

# ps -ef | grep padv
root      4550  2810  0 16:28 pts/5    00:00:00 padv -s adv.cfg
root      4639  2810  0 16:29 pts/5    00:00:00 grep padv

如何在我的 Perl 脚本中捕获该进程 ID?

How to capture that process ID in my Perl Script?

例如,我在 Perl 脚本中触发我的命令,如下所示:

For example, i am triggering my command in Perl script like below:

#!/usr/bin/perl

use strict;
use warnings;

qx(padv -s adv.cfg > adv.out &);

推荐答案

你可以使用 open()

Open 成功返回非零值,否则返回未定义值.如果打开涉及管道,则返回值恰好是子进程的 pid.

Open returns nonzero on success, the undefined value otherwise. If the open involved a pipe, the return value happens to be the pid of the subprocess.

my $pid = open(my $ph, "-|", "padv -s adv.cfg > adv.out") or die $!;

$ph 文件句柄读取输出而不是输出重定向:

reading output from $ph file handle instead of output redirect:

my $pid = open(my $ph, "-|", "padv -s adv.cfg") or die $!;

这篇关于如何获取我在 Perl 脚本中触发的 UNIX 命令的进程 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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