Klement Sekera | 94384e4 | 2017-07-11 07:29:37 +0200 | [diff] [blame] | 1 | #!/bin/bash |
2 | |||||
3 | rv=0 | ||||
4 | |||||
5 | atexit() { | ||||
6 | group_id=`ps -p $$ -o pgid=` | ||||
7 | my_id=$$ | ||||
8 | ids=`pgrep -g $group_id -d ' ' | sed "s/\b$my_id\b//g"` | ||||
9 | echo "Killing possible remaining process IDs: $ids" | ||||
10 | for id in $ids | ||||
11 | do | ||||
12 | if ps -p $id > /dev/null | ||||
13 | then | ||||
14 | kill -9 $id | ||||
15 | fi | ||||
16 | done | ||||
Klement Sekera | 8712ada | 2017-08-16 16:38:10 +0200 | [diff] [blame] | 17 | exit ${rv} |
Klement Sekera | 94384e4 | 2017-07-11 07:29:37 +0200 | [diff] [blame] | 18 | } |
19 | |||||
Klement Sekera | db4e84c | 2017-08-11 10:06:15 +0200 | [diff] [blame] | 20 | trap "atexit;" SIGINT SIGTERM |
Klement Sekera | 94384e4 | 2017-07-11 07:29:37 +0200 | [diff] [blame] | 21 | |
Klement Sekera | db4e84c | 2017-08-11 10:06:15 +0200 | [diff] [blame] | 22 | FORCE_FOREGROUND=$1 |
23 | shift | ||||
24 | |||||
25 | source $1 | ||||
26 | shift | ||||
27 | |||||
28 | if [[ "${FORCE_FOREGROUND}" == "1" ]] | ||||
29 | then | ||||
30 | $* | ||||
31 | else | ||||
32 | $* & | ||||
Klement Sekera | 8712ada | 2017-08-16 16:38:10 +0200 | [diff] [blame] | 33 | pid=$! |
34 | wait ${pid} | ||||
Klement Sekera | db4e84c | 2017-08-11 10:06:15 +0200 | [diff] [blame] | 35 | fi |
36 | |||||
Klement Sekera | 94384e4 | 2017-07-11 07:29:37 +0200 | [diff] [blame] | 37 | rv=$? |
38 | atexit | ||||
Klement Sekera | 8712ada | 2017-08-16 16:38:10 +0200 | [diff] [blame] | 39 | exit ${rv} |