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?