cleanupAndExit with trap #575

Closed
opened 2026-01-19 18:34:34 +00:00 by michael · 4 comments
Owner

Originally created by @kenchan0130 on GitHub.

While running the Installomator script, the process may be interrupted for some reason.
For example, the case where the "Self Service" of Jamf policy is canceled by an end-user while it is running.

In that case, the dmg mount and so on may continue to remain, so it would be good if cleanupAndExit can be executed even if the process is stopped.

The method doesn't have to use trap, but in a shell script, this kind of function has been originally achieved by trap.

Originally created by @kenchan0130 on GitHub. While running the Installomator script, the process may be interrupted for some reason. For example, the case where the "Self Service" of Jamf policy is canceled by an end-user while it is running. In that case, the dmg mount and so on may continue to remain, so it would be good if `cleanupAndExit` can be executed even if the process is stopped. The method doesn't have to use `trap`, but in a shell script, this kind of function has been originally achieved by `trap`.
michael added the featurehelp wantedwaiting for response labels 2026-01-19 18:34:34 +00:00
Author
Owner

@samerfarida commented on GitHub:

Hello @acodega,

We can actually use trap with some functions to print out the reason for termination.

The following snippet of code can be added to the top of the main Installomator script,

#!/bin/bash

trap_with_arg() {
    func="$1" ; shift
    for sig ; do
        trap "$func $sig" "$sig"
    done
}

func_trap() {
    printlog "Installomator has been terminated with: $1"
    cleanupAndExit "255"
}

trap_with_arg func_trap SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM SIGTSTP

### continue main script below ###
label="" # if no label is sent to the script, this will be used

# Installomator
#
# Downloads and installs Applications
# 2020-2021 Installomator
..........

Here is a sample of the logs when script interrupted by Control+C

2021-10-23 20:09:21 Latest version of Privileges is 1.5.2
2021-10-23 20:09:21 There is no newer version available.
2021-10-23 20:09:21 Using force to install anyway. Not using updateTool.
2021-10-23 20:09:21 Downloading https://github.com/SAP/macOS-enterprise-privileges/releases/download/1.5.2/Privileges.zip to Privileges.zip
2021-10-23 20:09:21 notifying
^C2021-10-23 20:09:22 Script has been terminated with: SIGINT
2021-10-23 20:09:22 Deleting /private/tmp/privileges.qRlENY
2021-10-23 20:09:22 App not closed, so no reopen.
2021-10-23 20:09:22 ################## End Installomator,  exit code 255

Here is a list of possible signals to be trapped except there are two signals which cannot be intercepted and handled: SIGKILL and SIGSTOP.

 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

Please let me know what are your thoughts on this?

Thank you

Sammy

Global Endpoint Security Engineer

@samerfarida commented on GitHub: Hello @acodega, We can actually use `trap` with some functions to print out the reason for termination. The following snippet of code can be added to the top of the main Installomator script, ``` #!/bin/bash trap_with_arg() { func="$1" ; shift for sig ; do trap "$func $sig" "$sig" done } func_trap() { printlog "Installomator has been terminated with: $1" cleanupAndExit "255" } trap_with_arg func_trap SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM SIGTSTP ### continue main script below ### label="" # if no label is sent to the script, this will be used # Installomator # # Downloads and installs Applications # 2020-2021 Installomator .......... ``` Here is a sample of the logs when script interrupted by Control+C ``` 2021-10-23 20:09:21 Latest version of Privileges is 1.5.2 2021-10-23 20:09:21 There is no newer version available. 2021-10-23 20:09:21 Using force to install anyway. Not using updateTool. 2021-10-23 20:09:21 Downloading https://github.com/SAP/macOS-enterprise-privileges/releases/download/1.5.2/Privileges.zip to Privileges.zip 2021-10-23 20:09:21 notifying ^C2021-10-23 20:09:22 Script has been terminated with: SIGINT 2021-10-23 20:09:22 Deleting /private/tmp/privileges.qRlENY 2021-10-23 20:09:22 App not closed, so no reopen. 2021-10-23 20:09:22 ################## End Installomator, exit code 255 ``` Here is a list of possible signals to be trapped except there are two signals which cannot be intercepted and handled: `SIGKILL` and `SIGSTOP`. ``` 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX ``` Please let me know what are your thoughts on this? Thank you Sammy Global Endpoint Security Engineer
Author
Owner

@acodega commented on GitHub:

Closing no response.

@acodega commented on GitHub: Closing no response.
Author
Owner

@acodega commented on GitHub:

Do either of you want to open a PR to add this support? You would want to do it against the dev branch, which we use for new non label related features.

@acodega commented on GitHub: Do either of you want to open a PR to add this support? You would want to do it against the `dev` branch, which we use for new non label related features.
Author
Owner

@acodega commented on GitHub:

I'm tagging this as help wanted as it could use input form others as to what the implications would be.

@acodega commented on GitHub: I'm tagging this as help wanted as it could use input form others as to what the implications would be.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Installomator/Installomator#575