For example when set -e terminates the shell due to a failed process or
the user sends SIGINT.
set -e
+# Terminate all running servers when the script terminates.
+trap 'stop_servers' 0
+# Same when the user presses Ctrl-C.
+trap 'abort SIGINT' INT
+
+
cleanup() {
rm -f \
tmp tlsproxy-log \
proxy-ca-key.pem proxy-ca.pem proxy-invalid.pem proxy-key.pem
}
stop_servers() {
- kill $pid_server || echo 'failed to kill gnutls-serv'
- kill $pid_tlsproxy || echo 'failed to kill tlsproxy'
+ if test -n "$pid_server"; then
+ kill $pid_server || echo 'failed to kill gnutls-serv'
+ fi
+ if test -n "$pid_tlsproxy"; then
+ kill $pid_tlsproxy || echo 'failed to kill tlsproxy'
+ fi
}
abort() {
echo "abort: $*"
test_invalid_certificate
-stop_servers
+# stop_servers in trap-handler
cleanup
exit 0
test_invalid_certificate
-stop_servers
+# stop_servers in trap-handler
cleanup
exit 0