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

Dear fellow monks,
I am trying to find out how you can grab the processID of any program that a Perl program can spawn. For example, this code calls gunzip. I want to know the PID of the gunzip process that I generated. I tried using $$, but that doesnt work. Here's my code. Any help will be most appreciated. Cheers! Robert
#!/usr/bin/perl $path_current = '/usr01/rzs96/logmining/logs'; print "Firing off process 1\n"; if (fork ()) { # RUN PARENT PROCESS print "PARENT = $$\n"; } else { # RUN CHILD PROCESS print "CHILD = $$\n"; close (STDOUT); # I need to get the PID from this gunzip process! `gunzip -c $path_current/access.webcache*.80.*-1200AM.gz | grep "0 +1225322" > Xtract-1.PID.log &`; }
  • Comment on How do you find the process ID to any UNIX programs that your Perl program executes?
  • Download Code

Replies are listed 'Best First'.
Re: How do you find the process ID to any UNIX programs that your Perl program executes?
by Joost (Canon) on Jul 12, 2005 at 16:27 UTC
    In this case, if you would do an exec() instead of a meaningless ` ` construct (you're not doing anything with the captured output, in fact, you're redirecting all possible output to a file), you would know the pid, because it would be the value returned by fork().

    Also, the open("|") type constructs return the child PID.

    update: and loose the & at the end of the syscall, you've already forked off a seperate process.

    update2: oh, I forgot. If you do pipes and redirect like you do, you're in fact starting a shell, which starts several other processes. So in fact, the only program started by perl would be the shell.

      Hey Joost, thank you for your very swift reply. You really are a credit to PerlMonks!
      I got the PID at last! Let's see now if this solution will help solve the larger challenge...after lunch of course!
      Cheers! Robert
Re: How do you find the process ID to any UNIX programs that your Perl program executes?
by fmerges (Chaplain) on Jul 12, 2005 at 16:30 UTC

    Hi,

    Why you don't use Compress::Zlib or some other module on CPAN?

    You are forking your process, so I don't understand why you are forking again...

    I would only fork one time, and load a module in the child process which would open the compressed file and grep it the Perl way.

    Updated: Seems like people prefer to put minus points before thinking about what really happen... so I explain it the long way

    You fork your main program for then launch the external commands using backsticks (``) but why you use they if you don't are using the output from the command inside the backsticks?

    Why you use gunzip and grep when you have modules and bultin functions that give you the same capabilities?

    Your external command call is insecure, Why? Because you are missing the ENVIRONMENT, besides some rules, one of the most important is not to execute programs without using the full path to the program:

    '/bin/gunzip' instead of 'gunzip'

    I know that this doesn't answer your question, but I think you can learn more from the answer I'd give you, than if I just answered yours.

    Take a look at perlsec and some basic documentation about secure sh scripting.

    Regards,

    |fire| at irc.freenode.net

    I like merlyn's disclaimer