Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

shell command to get pid

Posted on 1998-10-16
4
Medium Priority
?
642 Views
Last Modified: 2012-05-04
I'm trying to extract the process id of a process using a shell command.   I know how to get the line from ps using:

ps -e | grep process_name

I dont know how to further parse this output to just print the first field (i.e. the pid number).    Can anyone help?
0
Comment
Question by:aplatti
  • 2
4 Comments
 

Author Comment

by:aplatti
ID: 2007483
By the way, I tried using awk but the syntax is confusing the hell out of me.
0
 

Author Comment

by:aplatti
ID: 2007484
By the way, I tried using awk but the syntax is confusing the hell out of me.
0
 

Accepted Solution

by:
galemany earned 150 total points
ID: 2007485
ps -e|awk '$4 == PROC {print $1}' PROC=process_name

this works if ps -e gives an output like this:

 11602 ?          0:00 telnetd
 11604 pts/4    0:00 -sh
    877 ?          7:56 lpsched
    883 ?          0:20 lpNet
 11622 pts/4    0:00 ps
   1387 ?         0:03 nsrexecd
     912 ?         0:00 mountd  
0
 

Expert Comment

by:micaele
ID: 2007486
you like grep? Use it! :) See:

ps -e | grep process_name | sed 's/^ *\([0-9]*\) .*$/\1/'

Undestend me?
see also: man sed, man regex. Good luck!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question