LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-05-2005, 08:57 PM   #1
drj000
Member
 
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258

Rep: Reputation: 31
BASH script question


I need a way to launch a program in the background in a bash script and store the pid of that process (the program just launched, not the script itself) into a variable so that I can kill that process later on in the script.

I tried simply doing kill $$, but that terminates the script, without terminating the running program.
Any ideas?
 
Old 03-05-2005, 09:17 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

'$!' will contain the PID of the last command run.

Dave
 
Old 03-05-2005, 10:06 PM   #3
Antonius_Block
LQ Newbie
 
Registered: Nov 2004
Location: D.C.
Distribution: FC3,4
Posts: 11

Rep: Reputation: 0
try this:

program_name &
pid=$(/sbin/pidof program_name )
kill $pid

or alternatively:

program_name &
killall program_name
 
Old 03-06-2005, 12:37 AM   #4
drj000
Member
 
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258

Original Poster
Rep: Reputation: 31
$! didn't seem to work properly. It kept getting the wrong PID.
I had thought of killall program, but I didn't really want to do that, just in case I needed to run another instance of the same program, while the other was still running. I didn't know about /sbin/pidof, but that gives me the same problem as killall. If there are two processes of the same name, then pidof will list both process.
However, it does seem to work best out of all the other options. If I get the pid of the process soon after it's launched, then there will be less likelihood that I will have started another instance at that point. As long as I'm not already running said program, I'll be fine. Thanks for the help!
 
Old 03-06-2005, 08:37 AM   #5
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Weird.

$! works for me....
Code:
[dave@cronus ~]$ sleep 20000 &
[1] 30319
[dave@cronus ~]$ echo $!
30319
You have to make sure you get the PID out of $! immediately after you run the command you want the PID of.

Dave
 
Old 03-06-2005, 09:52 AM   #6
drj000
Member
 
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258

Original Poster
Rep: Reputation: 31
Quote:
Originally posted by ilikejam
Weird.

$! works for me....
You have to make sure you get the PID out of $! immediately after you run the command you want the PID of.
Yeah, well, what I was doing was the BASH script was running anothing script, a Python script, and the python script was running another program, and what I really needed was the bash script to get the pid of the program that the python script ran. Alhough when I posted my question, I didn't fully realize that that's what I really needed. Anyway, I think it may have been getting the pid too soon, and ended up getting the pid of the python script instead (or really, the python interpreter), but I'm not entirely sure.
In any case, when I time it properly, using pidof works perfectly.
 
Old 03-06-2005, 02:05 PM   #7
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Is there a way to get Python to, say, write the PID of the process it has started to a file, for the BASH script to retrieve (maybe to a named pipe to avoid synchronisation issues)?

I have no experience with Python, so I'm stabbing in the dark here.

Dave
 
Old 03-06-2005, 02:42 PM   #8
drj000
Member
 
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258

Original Poster
Rep: Reputation: 31
Quote:
Originally posted by ilikejam
Is there a way to get Python to, say, write the PID of the process it has started to a file, for the BASH script to retrieve (maybe to a named pipe to avoid synchronisation issues)?

I have no experience with Python, so I'm stabbing in the dark here.

Dave
That's it! I hadn't thought of that. I had to look it up, because I'm still learning python, but the os.spawn*() functions, with the NOWAIT mode will return the pid of the process it started, which I can pass to the bash script. That's perfect. Thanks for the idea! I'm going to test it out now.
 
Old 03-06-2005, 04:03 PM   #9
drj000
Member
 
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258

Original Poster
Rep: Reputation: 31
Quote:
Originally posted by ilikejam
Is there a way to get Python to, say, write the PID of the process it has started to a file, for the BASH script to retrieve (maybe to a named pipe to avoid synchronisation issues)?
Dangit, I couldn't get that to work either.
First I tried to make the python script pass the pid to the BASH script by simply printing it to standard out, and use the form pid=$(script.py), but to use that, script.py, and any processes launched by it have to be finished before it can get stdout. I tired "pid=$(script.py) &", but that returned a null value for $pid.
I then tried to use python's os.putenv() function to create an environment variable $pid, but the bash script had a null value for $pid, but if I typed echo $pid after the program was done, it would return the right pid.
Unless there's any other ideas, I guess I'll stick with using /sbin/pidof to get the pid.
 
Old 03-07-2005, 06:57 PM   #10
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Can Python not write to files?
 
Old 03-07-2005, 10:07 PM   #11
gbonvehi
Senior Member
 
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 51
Quote:
Originally posted by drj000
I then tried to use python's os.putenv() function to create an environment variable $pid, but the bash script had a null value for $pid, but if I typed echo $pid after the program was done, it would return the right pid.
Unless there's any other ideas, I guess I'll stick with using /sbin/pidof to get the pid.
The script already started and imported the enviroment values so the new assignment will be there for new programs only.

Why just not like ilikejam write the pid to a file from python, let's say pid.txt and then inside the your bash script:
Code:
pid=`cat pid.txt`
kill -KILL $pid
 
Old 03-07-2005, 11:25 PM   #12
drj000
Member
 
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258

Original Poster
Rep: Reputation: 31
Quote:
Originally posted by gbonvehi
The script already started and imported the enviroment values so the new assignment will be there for new programs only.

Why just not like ilikejam write the pid to a file from python, let's say pid.txt and then inside the your bash script:
Code:
pid=`cat pid.txt`
kill -KILL $pid
Yeah, I figured out why putenv didn't work. I didn't want to mess with creating files for a such a simple script. I know how to do it easily enought, but it seemed like a waste. /sbin/pidof works well enough. I haven't encountered any problems using it, so I'll just stick with that. Thanks for the suggestion, though.
 
  


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
Bash script question deiussum Programming 6 08-14-2004 12:43 PM
bash script question djgerbavore Linux - Newbie 3 07-08-2004 04:39 PM
bash script question xscousr Programming 5 07-03-2003 06:04 PM
Bash script question J_Szucs Linux - General 4 05-29-2003 09:48 AM
A bash script question J_Szucs Programming 2 05-13-2003 03:13 AM


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

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 Google+: linuxquestions
Open Source Consulting | Domain Registration