Solved

shell command to get pid

Posted on 1998-10-16
4
608 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now