LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 10-28-2014, 03:57 AM   #1
plotino
LQ Newbie
 
Registered: May 2014
Posts: 25

Rep: Reputation: Disabled
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
 
Old 10-28-2014, 04:02 AM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
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.
 
Old 10-28-2014, 04:11 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,460

Rep: Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765
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
 
Old 10-28-2014, 04:11 AM   #4
plotino
LQ Newbie
 
Registered: May 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-28-2014, 04:18 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
less elegant, but works:
Code:
killall $(pidof start_background_process)
obviously requires a unique name for the process.
 
Old 10-28-2014, 05:40 PM   #6
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
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:

Code:
man bash
 
Old 10-29-2014, 04:05 AM   #7
plotino
LQ Newbie
 
Registered: May 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-29-2014, 04:07 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,460

Rep: Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765
So as I posted, you can write
wait <pid>
or
wait %1
 
Old 10-30-2014, 04:18 AM   #9
plotino
LQ Newbie
 
Registered: May 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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
 
Old 10-30-2014, 01:23 PM   #10
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337

Rep: Reputation: 358Reputation: 358Reputation: 358Reputation: 358
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.
 
  


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
i need to run a shell script in background untill i kill it meabi Linux - Newbie 5 11-25-2010 05:54 PM
Background job won't stay in the background! JMJ_coder Linux - General 1 03-17-2008 07:05 PM
shel script questions shyamdey Programming 1 09-08-2006 03:09 AM
shel script shyamdey Programming 2 08-30-2006 08:49 AM
shel script resources b123coder Programming 1 06-28-2005 09:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

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