I have a Cygwin bash script that I need to watch and complete under certain conditions - in particular, after creating a specific file. Itβs hard for me to figure out exactly how to finish the script with the same level of completeness as Ctrl + C.
Here's a simple script (called ) that does a little more than wait for completion. test1
#!/bin/bash
test -f kill_me && rm kill_me
touch kill_me
tail -f kill_me
If this script is run in the foreground, Ctrl + C will interrupt both the tail
script and the script. If the script is running in the background, kill %1
(assuming that this task 1) will also complete both tail
, and the script.
However, when I try to do the same thing from a script, I find that only the bash
process that runs the script terminates and tail
hangs around disconnected from its parent. Here is one of the ways I tried ( test2
):
#!/bin/bash
test -f kill_me && rm kill_me
(
touch kill_me
tail -f kill_me
) &
while true; do
sleep 1
test -f kill_me && {
kill %1
exit
}
done
If this is done, the bash sub-element running in the background completes OK, but tail
still hangs around.
If I use an explicitly allocated script like this, it still doesn't work ( test3
):
#!/bin/bash
test -f kill_me && rm kill_me
./test1 &
while true; do
sleep 1
test -f kill_me && {
kill %1
exit
}
done
tail
still hanging after this script.
, ; , , , , , . , killall
, , .