LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-07-2015, 10:10 PM   #1
gaurav_s
Member
 
Registered: Jul 2014
Posts: 43

Rep: Reputation: Disabled
Best command to get pid of process.


What is the best way/command to get pid of process by process name?

I tried pgrep , ps -ef | grep procname, pidof but i don't think these are robust.
Sometimes i got more than one pid.
 
Old 01-07-2015, 11:29 PM   #2
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337

Rep: Reputation: 358Reputation: 358Reputation: 358Reputation: 358
Quote:
Originally Posted by gaurav_s View Post
What is the best way/command to get pid of process by process name?
Quote:
Sometimes i got more than one pid.
Logic says you have to expect that, when you have more than one process with the same name. The solution is to kill the specific process you want by pid, not by process name.

Last edited by haertig; 01-07-2015 at 11:30 PM.
 
Old 01-07-2015, 11:33 PM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Hi,
Quote:
Originally Posted by gaurav_s View Post
Sometimes i got more than one pid.
Some programs fork multiple processes and therefore will appear multiple time with different pids. For what programs are you seeing this?

Evo2.
 
Old 01-07-2015, 11:46 PM   #4
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 2,256

Rep: Reputation: 292Reputation: 292Reputation: 292
Does killall do what you want?
 
Old 01-08-2015, 12:14 AM   #5
gaurav_s
Member
 
Registered: Jul 2014
Posts: 43

Original Poster
Rep: Reputation: Disabled
i don't want to kill the process, i only want pid of process using process name.
 
Old 01-08-2015, 12:32 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,240

Rep: Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957
for me pgrep works. as it was mentioned an app can be started several times, therefore sometimes pgrep returns more pids. The most trivial example is the bash. If you want to check a specific instance you need to store its pid (when it was started).
 
Old 01-08-2015, 03:26 AM   #7
gaurav_s
Member
 
Registered: Jul 2014
Posts: 43

Original Poster
Rep: Reputation: Disabled
I am seeing this for customized applications.
 
Old 01-08-2015, 03:29 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Hi,
Quote:
Originally Posted by gaurav_s View Post
I am seeing this for customized applications.
Are there multiple instances running? Do they fork?

Evo2.
 
Old 01-08-2015, 03:33 AM   #9
gaurav_s
Member
 
Registered: Jul 2014
Posts: 43

Original Poster
Rep: Reputation: Disabled
ps -ef | grep app | grep -v grep |awk '{print $2}'

gives two pids.

ps -ef | grep app gives lot of lines (output). i want to minimize the output andi guess its forking.
 
Old 01-08-2015, 04:03 AM   #10
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Hi,
Quote:
Originally Posted by gaurav_s View Post
ps -ef | grep app | grep -v grep |awk '{print $2}'

gives two pids.
If there are two processes with the name "app" then that is what you should expect.
Quote:
Originally Posted by gaurav_s View Post
ps -ef | grep app gives lot of lines (output). i want to minimize the output
Minimize? To what aim?
Quote:
Originally Posted by gaurav_s View Post
andi guess its forking.
If the program forks, it forks... I really don't understand what you are trying to do. Perhaps we could be of more help if you give us the bigger picture.

Evo2.
 
Old 01-08-2015, 04:09 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,240

Rep: Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957
Quote:
Originally Posted by gaurav_s View Post
ps -ef | grep app | grep -v grep |awk '{print $2}'
That is exactly the same as pgrep app.
What do you mean by "customized applications"? What is the problem with those pids? Why do you need them?
 
Old 01-08-2015, 12:31 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,954
Blog Entries: 13

Rep: Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984
In my world:
Code:
ps -ef | grep <name>
Always finds the grep as well as any existing process names matching the search string. The grep process also matches the search string. I would not use the -f flag in that command. Perhaps you're looking for multiple users running that process though.
 
Old 01-08-2015, 01:48 PM   #13
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,316

Rep: Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331
This will only print the parent process(es) if it (or they) forked.

Code:
ps -ef | awk '/appname/ {if ($3 == 1) print $2}'
 
1 members found this post helpful.
Old 01-08-2015, 02:02 PM   #14
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337

Rep: Reputation: 358Reputation: 358Reputation: 358Reputation: 358
I'm not clear on exactly what your goal is here. Do you just want to identify a specific process so you can kill it? If so, that is easily done manually using commands that have been discussed here. Are you trying to write a foolproof script that will do automated kills without human interaction? You have to be a bit more careful doing that, although if you are running as a standard userid and not root, you've already got some protections built in due to permissions. Are you trying to snipe off and kill only certain spawned process of a master process, leaving the master and other siblings intact? You said you want to "minimize the output". That alone doesn't make any sense. Why do you want to minimize it? What do you want included and what do you want excluded from the output?

If you tell us exactly what you are trying to accomplish, I think you will get some good advice and help here.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to save the pid of a process at the time of process startup jhonnappier2007 Linux - Newbie 13 11-20-2011 11:54 PM
Can I assign a Command to a specific PID so I can kill a predetermined PID johnmccarthy Linux - Newbie 1 11-03-2011 08:41 AM
pid of a process Marin_ Linux - Software 3 03-07-2010 10:22 PM
how to get the name of process from PID in C/C++? kulandaivelu Programming 3 02-03-2009 09:41 AM
How to get the PID of the process giving kill signal to a process? hariprd Programming 2 11-27-2008 03:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:29 PM.

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