Skip to content

Commit

Permalink
Merge pull request #3 from jaygooby/master
Browse files Browse the repository at this point in the history
Use `pgrep` rather than `kill 0`
  • Loading branch information
pstadler committed Aug 26, 2016
2 parents cebe475 + 9e718bd commit 89c7128
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ while read -r line; do
done < <(dns-sd -B _rfb._tcp)

# kill child processes
kill -9 0 # SIGINT is not enough, let's send SIGKILL
kill -9 $(pgrep dns-sd) # SIGINT is not enough, let's send SIGKILL
```

Success! No more background processes after exit. However, there's this nasty problem with SIGKILL's verbose nature.
Expand Down Expand Up @@ -130,7 +130,7 @@ while read -r line; do
done < <(dns-sd -B _rfb._tcp)

# kill child processes
kill -13 0 # SIGPIPE to the rescue
kill -13 $(pgrep dns-sd) # SIGPIPE to the rescue
```

It does the trick. The child process gets killed while the termination message is suppressed:
Expand Down Expand Up @@ -163,14 +163,14 @@ while read -r line; do
if [ $(echo $line | cut -d ' ' -f 3) -ne '3' ]; then
break
fi
done < <((sleep 0.5; kill -13 0) & # kill quickly if trapped
done < <((sleep 0.5; pgrep -q dns-sd && kill -13 $(pgrep dns-sd)) & # kill quickly if trapped
dns-sd -B _rfb._tcp)

# kill child processes
kill -13 0
pgrep -q dns-sd && kill -13 $(pgrep dns-sd)
```

A new child process (`(sleep 0.5; kill -13 0) &`) is now running in the background followed by the `dns-sd` process. After 500ms it sends a SIGPIPE and the script will exit, no matter what. It's important to remember that any code after the loop is not executed in this case, as the script is terminated while still being trapped in the loop.
A new child process (`(sleep 0.5; pgrep -q dns-sd && kill -13 $(pgrep dns-sd)) &`) is now running in the background followed by the `dns-sd` process. After 500ms it sends a SIGPIPE and the script will exit, no matter what. It's important to remember that any code after the loop is not executed in this case, as the script is terminated while still being trapped in the loop.

## Do some work before termination

Expand Down Expand Up @@ -201,11 +201,11 @@ while read -r line; do
if [ $(echo $line | cut -d ' ' -f 3) -ne '3' ]; then
break
fi
done < <((sleep 0.5; kill -13 0) & # kill quickly if trapped
done < <((sleep 0.5; pgrep -q dns-sd && kill -13 $(pgrep dns-sd)) & # kill quickly if trapped
dns-sd -B _rfb._tcp)

# kill child processes
kill -13 0
pgrep -q dns-sd && kill -13 $(pgrep dns-sd)
exit 0
```

Expand Down
6 changes: 3 additions & 3 deletions discover-vnc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ while read -r line; do
if [ $(echo $line | cut -d ' ' -f 3) -ne '3' ]; then
break
fi
done < <((sleep 0.5; kill -13 0) & # kill quickly if trapped
done < <((sleep 0.5; pgrep -q dns-sd && kill -13 $(pgrep dns-sd)) & # kill quickly if trapped
dns-sd -B _rfb._tcp)

# kill child processes
kill -13 0
# kill dns-sd child process
pgrep -q dns-sd && kill -13 $(pgrep dns-sd)
exit 0

0 comments on commit 89c7128

Please sign in to comment.