Trapping the script

Using these signals:
```
SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM SIGTSTP
```

Fixed #272
This commit is contained in:
Søren Theilgaard
2022-02-08 13:11:08 +01:00
parent aea7d282fe
commit b42bd7e408
4 changed files with 21 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
## v10 ## v10
- Now we trap the script, so if it is cancelled, we will run cleanupAndExit anyway, and have mounted diskimages unmounted and temporary files and folders removed.
- -
## v9 ## v9

View File

@@ -1,4 +1,9 @@
# MARK: check minimal macOS requirement # MARK: 3 Arguments
# Start trapping the script
trap_with_arg func_trap SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM SIGTSTP
# check minimal macOS requirement
autoload is-at-least autoload is-at-least
installedOSversion=$(sw_vers -productVersion) installedOSversion=$(sw_vers -productVersion)
@@ -125,4 +130,4 @@ valuesfromarguments)
fi fi
;; ;;
# label descriptions start here # Mark: label descriptions start here

View File

@@ -1,4 +1,4 @@
# MARK: Functions # MARK: 2 Functions
cleanupAndExit() { # $1 = exit code, $2 message, $3 level cleanupAndExit() { # $1 = exit code, $2 message, $3 level
if [ -n "$dmgmount" ]; then if [ -n "$dmgmount" ]; then
@@ -29,6 +29,17 @@ cleanupAndExit() { # $1 = exit code, $2 message, $3 level
exit "$1" exit "$1"
} }
# Two functions to trap the script
trap_with_arg() {
func="$1" ; shift
for sig ; do
trap "$func $sig" "$sig"
done
}
func_trap() {
cleanupAndExit 255 "Installomator has been terminated with: $1" ERROR
}
runAsUser() { runAsUser() {
if [[ $currentUser != "loginwindow" ]]; then if [[ $currentUser != "loginwindow" ]]; then
uid=$(id -u "$currentUser") uid=$(id -u "$currentUser")

View File

@@ -1,3 +1,4 @@
# Mark: 4 Main
*) *)
# unknown label # unknown label
#printlog "unknown label $label" #printlog "unknown label $label"