Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,

I want to get the process id of the process where the actual process is executing.

 system("ls -lrt");

Here need to get the process id "ls -lrt" command.

How can I do that? Please help me...

Replies are listed 'Best First'.
Re: Obtain the child process id in perl
by moritz (Cardinal) on May 26, 2010 at 09:56 UTC
    With system I think you can't.

    Before the call to system the programm doesn't run yet, and the future PID unpredictable. After it is already finished, and the old PID useless.

    And why do you want that anyway? Looks like an XY Problem to me.

    And why would you spawn an ls process when you have opendir and readdir as well as glob or File::Find?

    Perl 6 - links to (nearly) everything that is Perl 6.

      For an example I used ls command there.

      My actual process is that,

      Server is keeping on running in one side. This server will execute another application which might be executed at min 10 minutes to get the data from the database or file. The application will create a file in common directory once the data is get from the file or database.

      Server will continue to do further process. It will not wait for any child process. It just checks file is available in the common directory.

      Here when the application is not written the file at that time I need to kill the process with the process id.

      For this how do I get the process id of the child process?

        You fork, and store the child PID in the parent. In the child you exec the application you want to run.

        Or you use IPC::Run, which abstracts such things away.

        See also: perlipc

        Perl 6 - links to (nearly) everything that is Perl 6.
Re: Obtain the child process id in perl
by cdarke (Prior) on May 26, 2010 at 11:51 UTC
    Use something like Proc::Simple which keeps track of the PID for you. There is even a kill method.