LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2008, 08:35 PM   #1
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Rep: Reputation: 15
Question pid of a process


How can I get only the PID of a process

like
ps -aef | grep "xyz"

It gives me pid but few other things also

I need only pid of process "xyz"

how can I do this?
 
Old 11-14-2008, 08:44 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
"man pidof"
 
Old 11-14-2008, 09:10 PM   #3
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Original Poster
Rep: Reputation: 15
pidof -s programname

it gives me the pid

I have to kill a process using shell script how can i do that

I tried the following
$a=pidof -s programname
kill -9 $a

but it's not working. Can anyone help me
 
Old 11-14-2008, 10:52 PM   #4
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
a=`/sbin/pidof -s programname`
kill -9 $a

also have a look at "killall"

Last edited by ArfaSmif; 11-14-2008 at 10:55 PM. Reason: added killall info
 
Old 11-14-2008, 11:53 PM   #5
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
i use pgrep... never even heard of pidof til now. anyone care to compare/contrast these two?
 
Old 11-15-2008, 12:42 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by abolishtheun View Post
i use pgrep... never even heard of pidof til now. anyone care to compare/contrast these two?
man pgrep
man pidof
 
Old 11-18-2008, 10:26 PM   #7
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks a lot.
 
Old 11-19-2008, 12:20 AM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by abolishtheun View Post
i use pgrep... never even heard of pidof til now. anyone care to compare/contrast these two?
As far as I know, pgrep/pkill come with kill, and should therefore be preferred for something like this. You need to watch out with pkill, though, because it will match a partial name, e.g. use pkill '^program$' instead of pkill program to prevent from killing myprogram, also.
ta0kira
 
Old 11-19-2008, 05:06 AM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
kill $(pidof myprogram)

jlinkels
 
  


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
Getting PID of running process in C/C++ laikos Programming 16 10-30-2015 06:16 PM
Getting pid of a process!! vishamr2000 Programming 34 03-12-2015 07:12 AM
[C++] Get process ID (PID) Moraxus Programming 5 10-16-2008 12:31 PM
how to get process status using pid? hongxing Linux - Software 2 04-11-2006 07:11 AM
Get Next PID of Process Tree zer0python Programming 7 11-26-2003 11:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:14 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