Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

How to get the process Id

by vasuperl (Acolyte)
on Jul 29, 2015 at 07:34 UTC ( #1136716=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I want to get the process id for the command executed in the command prompt. For example I want to execute command of "tcpdump -i any -w filename.pcap &" in the linux machine from my windows PC. Now I want to get the process id of that command. I have done telnet connection to that linux machine and able to execute that command. But i was not able to get the process id of that command to kill that process. I am new to perl. Please help me on this

Replies are listed 'Best First'.
Re: How to get the process Id
by Corion (Pope) on Jul 29, 2015 at 07:38 UTC

    This is more a shell question than a Perl question. The usual approach is to get a process which echoes its PID and then have that process replace itself with the target program, keeping the same PID.

    Shell scripts can do this using the following idiom:

    #!/usr/bin/ksh echo $$ exec tcpdump -i any -w filename.pcap

    This will print the PID to the console, but you could just as well output it to a file.

Re: How to get the process Id
by afoken (Canon) on Jul 29, 2015 at 16:32 UTC

    All attempts to get the PID using ps, pidof, or simular tools may suffer from race conditions and false positives. (Imagine someone else starting tcpdump with the same arguments, perhaps even using the same account.) Short: they are unreliable.

    Corion's way reliably writes the PID. But even that suffers from race conditions, because tcpdump may exit and its PID may be reused before the written PID is read by some other process.

    This is a general problem of stored PIDs. They may be outdated before they are read. The only safe way to work with stored PIDs is to catch SIGCHLD in the parent process and delete the stored PID when the child process exits.

    There are several tools working exactly this way, supervise from daemontools is one of them. supervise can reliably run a "background" process, and it can send signals to the "background" processes, all without having to write PID files. (The tool to send commands to supervise is svc.)


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: How to get the process Id
by vinoth.ree (Monsignor) on Jul 29, 2015 at 08:28 UTC

    After you started the command in the background you can use ps aux | grep tcpdumpcommand and get the process id of the process, first field is name of the owner who started the process, second filed is the process id(PID).

    All is well. I learn by answering your questions...
      If yours is not the only running tcpdump 'ps aux | grep tcpdump' returns more than one line. Luckily shell has $! variable that holds the last child PID. See bash for the details.
      Note that that's ps -ef unless bsd/bsd-compatible ps is installed.

      ETA: Never mind, Linux box, bsd compatible mode built in

      print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: How to get the process Id
by Myrddin Wyllt (Hermit) on Jul 29, 2015 at 13:11 UTC

    Instead of ps, you could use pgrep -n -u vasuperl tcpdump

    You need to do less munging of the return value as it just gives you the process id of the last command (-n) containing the string 'tcpdump' executed by the user (-u) 'vasuperl'.

Re: How to get the process Id
by i5513 (Pilgrim) on Jul 29, 2015 at 20:21 UTC

    Reply OT (not perl is mentioned)

    I'm not sure if this is a XY Problem, but if you want to capture the traffic on remote host from windows, I would install plink + pageant from putty page and tshark from wireshark home

    I would setup my environment so plink can ssh to host without password, with pki infraestructure (see pageant doc) (but you can use the the insecure -pw plink option) (thought I would try to use sudo and not root direct access)

    Then add to %PATH% env var paths to tshark.exe and to plink.exe, and you could execute, to save 3 seconds of remote network traffic:

    plink root@host tcpdump -i any -s0 -w - not port 22 | tshark -i - -w r +emote-traffic-captured.dump -a duration:3
Re: How to get the process Id
by M4 (Initiate) on Jul 29, 2015 at 15:10 UTC

    You may be helped by a simple killall tcpdump, if yours is the only tcpdump running on the machine.

    Otherwise, you may need to write a little wrapper program around the tcpdump command to fork and 1) In the parent get the pid (it's the return value from fork) and store it somewhere; 2) In the child exec() the tcpdump.

    HTH, M4

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1136716]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2020-07-04 10:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found