1
- #! /usr/bin/env sh
1
+ #! /usr/bin/env bash
2
2
3
3
# Exit if any command fails
4
4
set -e
@@ -59,6 +59,16 @@ redis_clear() {
59
59
}
60
60
61
61
main_native () {
62
+ # check if Ctrl+C is pressed
63
+ (
64
+ while true ; do
65
+ sleep 1
66
+ kill -0 $$ || break
67
+ done
68
+ cleanup ${services}
69
+ )&
70
+ pid=$!
71
+
62
72
local services=${SERVICES}
63
73
local postgres_url_env_var=` [[ $services == * " db_test" * ]] && echo " DB_TEST_POSTGRES_URL" || echo " DB_POSTGRES_URL" `
64
74
local redis_host_env_var=` [[ $services == * " redis_test" * ]] && echo " REDIS_TEST_HOST" || echo " REDIS_HOST" `
@@ -82,6 +92,7 @@ main_native() {
82
92
fi
83
93
84
94
cleanup () {
95
+ kill ${pid} & > /dev/null
85
96
local services=$@
86
97
87
98
if [ -n " ${redis_host} " ] && [[ $services == * " redis_test" * ]]; then
@@ -93,25 +104,28 @@ main_native() {
93
104
fi
94
105
}
95
106
96
- # trap SIGINT and performs cleanup
97
- trap " on_sigint ${services} " INT
98
- on_sigint () {
99
- cleanup $@
100
- exit $?
101
- }
102
-
103
107
# Run the arguments as a command
104
108
DB_POSTGRES_URL=" ${postgres_url} " \
105
109
REDIS_HOST=" ${redis_host} " \
106
110
" $@ "
107
111
code=$?
108
112
109
113
cleanup ${services}
110
-
114
+ sleep 1
111
115
exit ${code}
112
116
}
113
117
114
118
main_docker () {
119
+ # check if Ctrl+C is pressed
120
+ (
121
+ while true ; do
122
+ sleep 1
123
+ kill -0 $$ || break
124
+ done
125
+ echo " Ctrl+C detected"
126
+ cleanup ${services}
127
+ )&
128
+ pid=$!
115
129
# Expect a SERVICES env var to be set with the docker service names
116
130
local services=${SERVICES}
117
131
@@ -124,20 +138,14 @@ main_docker() {
124
138
# performs cleanup as necessary, i.e. taking down containers
125
139
# if this script started them
126
140
cleanup () {
141
+ kill ${pid} & > /dev/null
127
142
local services=$@
128
143
echo # newline
129
144
if $started_container ; then
130
145
docker compose --file $compose_file rm --force --stop --volumes ${services}
131
146
fi
132
147
}
133
148
134
- # trap SIGINT and performs cleanup
135
- trap " on_sigint ${services} " INT
136
- on_sigint () {
137
- cleanup $@
138
- exit $?
139
- }
140
-
141
149
# check if all services are running already
142
150
not_running=false
143
151
for service in $services ; do
@@ -167,6 +175,7 @@ main_docker() {
167
175
168
176
# performs cleanup as necessary
169
177
cleanup ${services}
178
+ sleep 1
170
179
exit ${code}
171
180
}
172
181
0 commit comments