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 |
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-05-2005, 07:57 PM
|
#1
|
|
Member
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258
Rep:
|
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?
|
|
|
|
03-05-2005, 08:17 PM
|
#2
|
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
Hi.
'$!' will contain the PID of the last command run.
Dave
|
|
|
|
03-05-2005, 09:06 PM
|
#3
|
|
LQ Newbie
Registered: Nov 2004
Location: D.C.
Distribution: FC3,4
Posts: 11
Rep:
|
try this:
program_name &
pid=$(/sbin/pidof program_name )
kill $pid
or alternatively:
program_name &
killall program_name
|
|
|
|
03-05-2005, 11:37 PM
|
#4
|
|
Member
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258
Original Poster
Rep:
|
$! 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!
|
|
|
|
03-06-2005, 07:37 AM
|
#5
|
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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
|
|
|
|
03-06-2005, 08:52 AM
|
#6
|
|
Member
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258
Original Poster
Rep:
|
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.
|
|
|
|
03-06-2005, 01:05 PM
|
#7
|
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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
|
|
|
|
03-06-2005, 01:42 PM
|
#8
|
|
Member
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258
Original Poster
Rep:
|
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.
|
|
|
|
03-06-2005, 03:03 PM
|
#9
|
|
Member
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258
Original Poster
Rep:
|
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.
|
|
|
|
03-07-2005, 05:57 PM
|
#10
|
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
Can Python not write to files?
|
|
|
|
03-07-2005, 09:07 PM
|
#11
|
|
Senior Member
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145
Rep:
|
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
|
|
|
|
03-07-2005, 10:25 PM
|
#12
|
|
Member
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258
Original Poster
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:14 AM.
|
|
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
|
|