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

How to obtain current child process name executing on Unix box using perl script?
  • Comment on How to obtain current child process name?

Replies are listed 'Best First'.
Re: How to obtain current child process name?
by merlyn (Sage) on Nov 30, 2009 at 11:07 UTC
    Since Unix processes have numbers, but not names, I'm curious as to what problem you are trying to solve, the solving of which seems to need to know the "name" of a Unix process, which doesn't exist.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      there are application, that change name to show current status. Maybe OP has that kind of application and wants to read that status (to detect deadlock, for example).
Re: How to obtain current child process name?
by doug (Pilgrim) on Nov 30, 2009 at 14:49 UTC

    This seems to be a poorly defined question. Unix processes do not have "names" per se, they simply have unique id number called a pid. The name of the executable is kinda like what you want, but every bash process will be called "bash" or "/usr/bin/bash". There is no uniqueness to this, so it isn't really a name.

    You might want to take a step back and look to see what it is that you really want to do, and let us know what that is. You're more likely to get a useful answer that way.

    If you want to go this alone, look at the output of "/bin/ps", especially "ps -aef" and grep for whatever it is that you want. You might find a bit more info in /proc/<PID> but that isn't guaranteed to be portable, which may or may not be a concern for you.

Re: How to obtain current child process name?
by colwellj (Monk) on Nov 30, 2009 at 05:49 UTC
    see fork
    Returns the child PID to the parent process and 0 to the child
      Hi, Thanx for the reply. I am able to obtain the child PID. But i am not able to figure out the process name from child PID.I am interested in its process name.
Re: How to obtain current child process name?
by shmem (Chancellor) on Nov 30, 2009 at 12:06 UTC

    See perlvar. It's in $0; you can assign to it.

    qwurx [shmem] ~ > perl -e '$0="foobar"; sleep'

    and in another window

    qwurx [shmem] ~ > ps auxw | grep foobar shmem 4041 0.0 0.0 6012 1312 pts/1 S+ 13:00 0:00 fooba +r shmem 4043 0.0 0.0 4220 584 pts/2 S+ 13:00 0:00 grep +foobar
      That's the process's own name. OP seems to be trying to find the name of a child that he forked off. From the discussion so far, the more general case appears to be "Given the pid of a process, how do I determine its name?"
        That's the process's own name. OP seems to be trying to find the name of a child that he forked off.

        Yes indeed, and the forked-off process - is a process, with its name residing in $0. The parent wants to know it for some reason. It could:

        1. ask the kernel almighty
        2. ask his offspring to tell, and listen to what it says.

        Solving the first would be the "more general case" you mention. Solving the second possibly involves inter process communication (perlipc).

Re: How to obtain current child process name?
by zentara (Cardinal) on Nov 30, 2009 at 17:27 UTC
    ... you may be looking for something like
    #!/usr/bin/perl -w use strict; use Proc::ProcessTable; my $t1 = new Proc::ProcessTable; my $pid; my $commandline = shift || $0; foreach my $p (@{$t1->table}){ # reverse this logic if($p->cmndline =~ /\Q$commandline\E/){ $pid = $p->pid; print "$pid\n"; } }
    .... but if you are on linux, and know the pid, then just read the file "/proc/$pid/cmdline"

    ... the problem becomes this.... the $pid you have in hand may not be the actual child, but instead the shell running the child..... look up killfam and see Killing children's of children for examples of the problem


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku