Solved

shell command to get pid

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

746 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

14 Experts available now in Live!

Get 1:1 Help Now