LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-23-2010, 07:27 AM   #1
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Rep: Reputation: 62
Bash script to get PID of a specific process.


I'm trying to write a script that involves storing a specific PID in a variable. I've got as far as: -

Code:
$PS_OUTPUT=ps -elf | grep <process name> | awk '{print $4}'
This stored the PID of the grep itself and the actual process I want. The output of $PS_OUTPUT is: -

Code:
3548
10747
I only want the second number as the first is the PID of the grep. I either want to suppress the PID of the grep from the output or just grab the second PID. Since the output is on two lines, I thought there must be a newline (\n) character in there somewhere, but if I use: -

Code:
ps -elf | grep <process name> | awk '{print $4}' | awk -F"\n" '{print $2}'
...I get nothing and {print $1} replaced in the above gives me both PIDs again on two lines. Which character is outputting the "ps" command on two lines that isn't a "\n"? Can I somehow write a regex that will exclude the PID of the grep itself?

Thanks for any help.
 
Old 08-23-2010, 07:34 AM   #2
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
Is this what you are looking for ?

Code:
$PS_OUTPUT=`ps -elf | grep <process name> | grep -v grep | awk '{print $4}`
 
1 members found this post helpful.
Old 08-23-2010, 07:36 AM   #3
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Original Poster
Rep: Reputation: 62
Perfect, thank you! I didn't know about the -v (invert) switch for grep. That will be very useful in future.
 
Old 08-23-2010, 07:37 AM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Or, you can use
Code:
pidof <process name>
that will give just the PID
 
1 members found this post helpful.
Old 08-23-2010, 07:38 AM   #5
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Original Poster
Rep: Reputation: 62
Ah, that's good also Didn't know about that command.
 
Old 08-23-2010, 07:40 AM   #6
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 71

Rep: Reputation: 25
Why don't you use pgrep?


Code:
thanatos:~ # pgrep cron
3396
 
1 members found this post helpful.
Old 08-23-2010, 07:45 AM   #7
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Original Poster
Rep: Reputation: 62
Cool - didn't know that one either Does the same job as pidof by the looks of it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
scripting, perl or bash; run a background process, get pid 2ck Programming 6 04-02-2010 12:16 AM
how to retreive PID of specific python script using pidof Mash Linux - Newbie 1 03-18-2009 10:28 PM
Bash: check pid for active process (FreeBSD) Harpune Programming 2 05-16-2006 01:33 PM
How do I get PID info about a specific script? Stepaj47 Linux - Newbie 2 11-14-2005 04:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration