LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
LinkBack Search this Thread
Old 07-25-2005, 08:25 PM   #1
sceadu
Member
 
Registered: Jan 2003
Posts: 76

Rep: Reputation: 15
Bash programming - using pidof


Hi,
I'm writing a bash script to kill a process.

This is what I wrote:

#!/bin/sh

pidof -s 'proc_to_be_killed' > /dev/null

echo $?

if [ $? -eq "0" ]
then
echo "proc_to_be_killed is not running"
else
kill $?
echo "proc_to_be_killed stopped"
fi


However, I always get 0 from $?. If I run pidof -s 'proc_to_be_killed' manually from command, it always return the process id(not 0).

How do I capture the process id as it seems that $? is not giving me the process id returned from running pidof.

Please advise.
Thanks.
 
Old 07-25-2005, 08:29 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: Kennesaw, GA
Distribution: Ubuntu
Posts: 8,502

Rep: Reputation: 110Reputation: 110
I'd like to point out that in your if statement, you are testing the return value of echo, not pidof.

Additionally, nowhere do I find it documented that pidof should return the process id as an exit code, but instead, it prints out the process id.
 
Old 07-25-2005, 08:30 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
$? is the shell final/exit status of the last cmd executed.
Usually set to zero if ok, else some num for various errors etc.
Try assigning the output of pidof eg
proc_id=`pidof -s proc_to_be_killed`
NB: those are backquotes (top-left on keybd), not single quotes.
 
Old 07-26-2005, 01:02 AM   #4
sceadu
Member
 
Registered: Jan 2003
Posts: 76

Original Poster
Rep: Reputation: 15
Thanks.
 
Old 07-26-2005, 01:03 AM   #5
Matir
Moderator
 
Registered: Nov 2004
Location: Kennesaw, GA
Distribution: Ubuntu
Posts: 8,502

Rep: Reputation: 110Reputation: 110
you're welcome.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Programming moodupani Programming 1 08-31-2005 06:50 PM
bash programming alaios Programming 2 07-23-2005 12:52 AM
bash programming drisay Programming 6 12-20-2004 09:09 AM
bash programming cpukiller Linux - Newbie 1 11-04-2003 06:45 PM
bash programming bulliver Programming 2 04-05-2003 09:30 PM


All times are GMT -5. The time now is 11:09 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration