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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-07-2015, 10:10 PM
|
#1
|
Member
Registered: Jul 2014
Posts: 43
Rep: 
|
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.
|
|
|
01-07-2015, 11:29 PM
|
#2
|
Senior Member
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337
|
Quote:
Originally Posted by gaurav_s
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.
|
|
|
01-07-2015, 11:33 PM
|
#3
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Hi,
Quote:
Originally Posted by gaurav_s
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.
|
|
|
01-07-2015, 11:46 PM
|
#4
|
Senior Member
Registered: Mar 2010
Distribution: Slackware
Posts: 2,256
|
Does killall do what you want?
|
|
|
01-08-2015, 12:14 AM
|
#5
|
Member
Registered: Jul 2014
Posts: 43
Original Poster
Rep: 
|
i don't want to kill the process, i only want pid of process using process name.
|
|
|
01-08-2015, 12:32 AM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,240
|
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).
|
|
|
01-08-2015, 03:26 AM
|
#7
|
Member
Registered: Jul 2014
Posts: 43
Original Poster
Rep: 
|
I am seeing this for customized applications.
|
|
|
01-08-2015, 03:29 AM
|
#8
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Hi,
Quote:
Originally Posted by gaurav_s
I am seeing this for customized applications.
|
Are there multiple instances running? Do they fork?
Evo2.
|
|
|
01-08-2015, 03:33 AM
|
#9
|
Member
Registered: Jul 2014
Posts: 43
Original Poster
Rep: 
|
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.
|
|
|
01-08-2015, 04:03 AM
|
#10
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Hi,
Quote:
Originally Posted by gaurav_s
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
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
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.
|
|
|
01-08-2015, 04:09 AM
|
#11
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,240
|
Quote:
Originally Posted by gaurav_s
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?
|
|
|
01-08-2015, 12:31 PM
|
#12
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,954
|
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.
|
|
|
01-08-2015, 01:48 PM
|
#13
|
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,316
|
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.
|
01-08-2015, 02:02 PM
|
#14
|
Senior Member
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337
|
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.
|
|
|
All times are GMT -5. The time now is 02:29 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|