I want to kill a single background process in bash
$SCRIPT_DIR/utils/monitor.sh -f $root/save &
$SCRIPT_DIR/utils/run.sh -f $save
$SCRIPT_DIR/utils/Monkey.sh -f $save
I want to kill monitor.sh after finishing Monkey.sh.
I tried using pid but its not working.
It looks like the problem might be related to how monitor.sh is called. If I have a script 'foo.sh' that just has a shell command in it, I won't see it identified as 'foo.sh' in a ps
listing, but if I call it with sh
(or bash
) then I do. It seems that if 'foo.sh' is a list of shell commands, the shell will change that to bash , and you won't see 'foo.sh' in the ps
listing, but if I explicitly call it with bash, i.e.
bash foo.sh
then I see it in the ps listing.
However, best practice for shell scripts, is to start off with the appropriate 'hashbang' command, i.e. for a bash script the first line should be
#!/bin/bash
this also seems to fix the problem for me. I'm guessing that this line may be missing from monitor.sh, and that's why you don't see it using ps
or killall
. Once that's in place, you should be able to do
killall -9 monitor.sh
or similar and it will work. Either that or invoke it as bash monitor.sh
, but common best practice is to include that first line either way.