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.
|
|
10-28-2014, 03:57 AM
|
#1
|
LQ Newbie
Registered: May 2014
Posts: 25
Rep:
|
how to kill a background job from within a shel script
Hello
I have written a scritp that starts a process in backgroung
(using the ampersend character).
After the script is run, the job is started, but at the moment i have to use 'ps' commmand to get the PID and use manually the kill command.
I would like to automate the exit action for the process.
How to include the close command for the process from within the script?
thanx
plotino
|
|
|
10-28-2014, 04:02 AM
|
#2
|
Member
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705
Rep:
|
Hi plotino!
If you are using bash, the variable $! contains the pid of the last background process that was run.
If you are using some other shell, please let us know which one it is.
HTH.
|
|
|
10-28-2014, 04:11 AM
|
#3
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,460
|
yes, the solution is $!:
Code:
start_background_process &
BG_PID=$!
... do whatever you want ...
# kill your background process:
kill -s <signal> $BG_PID
# or alternatively you may use:
kill -s <signal> %1
# wait for completition:
wait $BG_PID
# or
wait %1
|
|
|
10-28-2014, 04:11 AM
|
#4
|
LQ Newbie
Registered: May 2014
Posts: 25
Original Poster
Rep:
|
thanks rigor.
im using bash.
so the question now is how to exploit the variable $! at the end of the script to close the backgrounded process?
can i associate it to some keys? i dont know how ...
thanks pan64,
the point is that running the scipt, you come back to the shell, where the running and backgroundede process gives some log infos.
during this, at any time, i would like to stop the process for example with a key combination
Last edited by plotino; 10-28-2014 at 04:21 AM.
|
|
|
10-28-2014, 04:18 PM
|
#5
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
less elegant, but works:
Code:
killall $(pidof start_background_process)
obviously requires a unique name for the process.
|
|
|
10-28-2014, 05:40 PM
|
#6
|
Member
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705
Rep:
|
plotino,
I realize that there can be a tendency to think that abstracting a question from the details surrounding it, asking it in the abstract, may be simplifying the issue, but sometimes it actually makes getting the answer more difficult, or at least more of a protracted procedure. Especially because the details may change the answer.
As for the more specific situation that you are now describing, a trivial example of a potential solution, depending on whether or not there are additional relevant details:
Code:
trap 'kill $!' 2
sleep 900 &
sleep 60
when I run the above bash script, then use my "Interrupt" key to kill the foreground sleep, the trap hook will execute the kill command to kill the background sleep.
These and other mysteries can be solved by running:
|
|
|
10-29-2014, 04:05 AM
|
#7
|
LQ Newbie
Registered: May 2014
Posts: 25
Original Poster
Rep:
|
thanks rigor,
you are right. need more details.
my script is..
Code:
"process" &
command1
command2
so, it put process in bg, run command1 and command2, and after it exits.
Adding
Code:
trap "kill $!" 2
"process" &
command1
command2
i need to not exit from the script to be able to kill the bg process, isnt'it?
so i have to add a wait(forever) instruction at the end.
this my supposition, please tell me if im right .
ciao
plotino
|
|
|
10-29-2014, 04:07 AM
|
#8
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,460
|
So as I posted, you can write
wait <pid>
or
wait %1
|
|
|
10-30-2014, 04:18 AM
|
#9
|
LQ Newbie
Registered: May 2014
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
So as I posted, you can write
wait <pid>
or
wait %1
|
ok thank you.
i have resolved with this:
Code:
trap 'kill $!' 2
<process> &
command1
command2
sleep 10000
ciao
plotino
|
|
|
10-30-2014, 01:23 PM
|
#10
|
Senior Member
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337
|
What is your ultimate goal? From your description, it appears that you are starting a process that assumedly will take a while to run. So you start it in the background. Fair enough. Then you run two other commands that I assume completely quickly. When those last two commands are done, you want to then go kill the original backgrounded process, whether it's done or not? Or possibly the background process runs forever and generates information that is consumed by the latter two processes, and once those latter two processes are complete, you no longer need the backgrounded process?
I don't understand your goal. If you want to kill that backgound process right after the two other commands have completed, a solution has already been suggested. But if you want to wait until that background process has completed, then kill it, that's what I'm not understanding. If it's already completed, then it's done, dead, in its grave already. You don't have to kill it a second time.
|
|
|
All times are GMT -5. The time now is 02:45 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
|
|