LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 07-26-2014, 12:01 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
bash script how kill app running in bg that was started by the script


If I start an app running in a bash script, I have to start it in bg in order for the script to continue processing. After a certain time I want the script to terminate that app gracefully, but I find that neither kill nor kill -9 issued in the script has any effect on that app. How then can I do it?
Here is the script
Code:
~ $ declare -f nt
nt ()
{
    while [ 0 ]; do
        y=$(date +%H%M);
        w=($(ps ax|grep rtorrent|grep -v grep));
        if [[ "${w[4]}" != "rtorrent" ]]; then
            rtorrent & echo "$(ps ax|grep rtorrent|grep -v grep) started $(date)";
            sleep 30;
        fi;
        if [[ "$(date +%H%M)" > "$y" ]]; then
            if [[ "${w[4]}" == "rtorrent" ]]; then
                kill -9 ${w[0]} && echo "${w[0]} killed $(date)";
            fi;
        fi;
        sleep 30;
    done
}
And here is what happens when I run it
Code:
~ $ nt
[1] 20101
20101 pts/4    D      0:00 rtorrent started Sat Jul 26 09:31:26 PDT 2014

[1]+  Stopped                 rtorrent
The system claims that the kill -9 has taken effect, but notice that the '&& echo ...' tied to its successful completion does not show. After 2 minutes I do
Code:
^C
~ $ ps ax|grep rtorrent|grep -v grep
20101 pts/4    Tl     0:00 rtorrent
~ $ kill 20101 && sleep 0.1
~ $ ps ax|grep rtorrent|grep -v grep
20101 pts/4    Tl     0:00 rtorrent
~ $ sleep 60
~ $ ps ax|grep rtorrent|grep -v grep
20101 pts/4    Tl     0:00 rtorrent
~ $ kill -9 20101
~ $ ps ax|grep rtorrent|grep -v grep
[1]+  Killed                  rtorrent
~
Finally I get rid of it with kill -9 from the tty. Why does it work there but not in the script? How can I terminate rtorrent gracefully in the script?
 
Old 07-26-2014, 12:43 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

IMHO:
Instead of
Code:
while [ 0 ]; ...
I'd prefer
Code:
while :; ...
Instead of
Code:
grep rtorrent | grep -v grep
one may use
Code:
grep [r]torrent
Now, go back to kill (not literally ): it looks like this condition
Code:
[[ "$(date +%H%M)" > "$y" ]];
is never true because the value of $y is the same as $(data +%H%M).
 
1 members found this post helpful.
Old 07-26-2014, 01:18 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Angry

Quote:
Originally Posted by firstfire View Post

Now, go back to kill (not literally ): it looks like this condition
Code:
[[ "$(date +%H%M)" > "$y" ]];
is never true because the value of $y is the same as $(data +%H%M).
This is true, thank you

Last edited by porphyry5; 07-26-2014 at 01:53 PM.
 
  


Reply



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
How to kill some runnin perl script from bash script? postcd Linux - General 2 05-01-2014 06:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
Started programs with one script. Want to kill them with another halfpower Programming 3 11-02-2010 12:45 AM
[SOLVED] How to make a bash script keep running in same terminal after it calls second script? JohnRock Linux - Newbie 4 06-25-2010 09:16 AM
bash - start app, [do stuff], kill started app MiniMe001 Programming 3 03-19-2008 03:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:27 AM.

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