The Linux Administration group is for the discussion of technical issues technical issues that arise during the administration of Linux systems, including maintaining the operating system and supporting end-user applications.

Finding PID in Single Line?

I’m looking for a PID for the program currently running, so I had initially done this: "ps aux | grep program_name". My results were root 12347 2.0 2.0 1588 244 pts/1 s+ Dec01 0.00 program_name and root 25317 0.0 0.0 1673 628 pts/5 s+ 13:25 0:00 grep program_name. My question is, how do I grep to excluse the end (grep program_name) and extract the PID (12347) and combine ps aux and grep in a single command line to make this easier?

Question from aaronheaney on Dec 12 at 1:11 PM
I'm looking for a PID for the program currently running, so I had initially done this: "ps aux | grep program_name". My results were root 12347 2.0 2.0 1588 244 pts/1 s+ Dec01 0.00 program_name and root 25317 0.0 0.0 1673 628 pts/5 s+ 13:25 0:00 grep program_name. My question is, how do I grep to excluse the end (grep program_name) and extract the PID (12347) and combine ps aux and grep in a single command line to make this easier?

.. It can return several pids (as when several copies are running). How would you expect to deal with this case?

.. It has an option to return at most one pid, in which case it is presumably a random choice.

.. It relies on the program name matching. It can't tell the difference between /bin/foo and ~/local/foo unless it gets the full path as an argument.

.. It also seems flaky on symbolic links and scripts.

It is really a lot better to have instances of background jobs make a file to indicate their pid.

.. If you only permit one copy of the program, have it use a file called /tmp/MyAppIden.pid as a lock file, and echo the actual pid and start date into it.

.. If you permit multiple copies, have each copy use a file called /tmp/MyAppIden.12735 where the number is its pid, so the filename is unique. Then you can find all the pids with ls, and not have issues with ps.

You will get all the PID numbers of running sendmail command in a list and
then you could use any one PID for further processing.
You could also pick the PID number which you want. if you could provide us
details.