Solved

shell command to get pid

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to backup and restore the DHCP configuration on Solaris 9 7 58
pauing printer deamon in AIX 10 63
How to find files recursively in ftp server quickly? 7 124
MarkLogic 1 89
Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

830 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