?
Solved

shell command to get pid

Posted on 1998-10-16
4
Medium Priority
?
627 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 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.:
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

777 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