How can i get process id of UNIX command i am triggering in a Perl script?

前端 未结 2 455
北海茫月
北海茫月 2021-01-15 13:18

I am triggering a UNIX command in Perl script.

I need the process ID of the UNIX command.

For example if i trigger below UNIX command:

# padv         


        
相关标签:
2条回答
  • 2021-01-15 13:50

    You could use open()

    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 $!;
    

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

    my $pid = open(my $ph, "-|", "padv -s adv.cfg") or die $!;
    
    0 讨论(0)
  • 2021-01-15 14:00

    Call fork to create a child process. The process ID of the child process is returned to the parent process. The child process can then call exec to execute the program you want.

    0 讨论(0)
提交回复
热议问题