Skip to content

Commit

Permalink
Merge pull request #37 from mpanighetti/restart-fix
Browse files Browse the repository at this point in the history
fixed broken restart behavior
  • Loading branch information
mpanighetti authored Jan 22, 2020
2 parents 6b5a99b + 0ac8e72 commit 90af1a6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ All notable changes to this project will be documented in this file. This projec
Nothing yet.


## [2.3.4] - 2020-01-21

### Changed

- `clean_up` function no longer unloads primary LaunchDaemon ahead of triggering system restart or shutdown #33
- moved primary LaunchDaemon unload to `exit_without_updating`
- `clean_up` function moves all script resources to `/private/tmp/install-or-defer`


## [2.3.3] - 2019-10-15

### Added
Expand Down Expand Up @@ -150,7 +159,8 @@ Nothing yet.
- Initial release


[Unreleased]: https://github.com/homebysix/install-or-defer/compare/v2.3.3...HEAD
[Unreleased]: https://github.com/homebysix/install-or-defer/compare/v2.3.4...HEAD
[2.3.4]: https://github.com/homebysix/install-or-defer/compare/v2.3.3...v2.3.4
[2.3.3]: https://github.com/homebysix/install-or-defer/compare/v2.3.2...v2.3.3
[2.3.2]: https://github.com/homebysix/install-or-defer/compare/v2.3.1...v2.3.2
[2.3.1]: https://github.com/homebysix/install-or-defer/compare/v2.3.0...v2.3.1
Expand Down
2 changes: 1 addition & 1 deletion build-info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
<key>suppress_bundle_relocation</key>
<true/>
<key>version</key>
<string>2.3.3</string>
<string>2.3.4</string>
</dict>
</plist>
74 changes: 49 additions & 25 deletions payload/Library/Scripts/install_or_defer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# restarts automatically.
# Authors: Elliot Jordan and Mario Panighetti
# Created: 2017-03-09
# Last Modified: 2019-10-15
# Version: 2.3.3
# Last Modified: 2020-01-21
# Version: 2.3.4
#
###

Expand All @@ -34,6 +34,9 @@ LOGO=""
# ".plist" extension.
BUNDLE_ID="com.elliotjordan.install_or_defer"

# The file path of this script.
SCRIPT_PATH="/Library/Scripts/install_or_defer.sh"


################################## MESSAGING ##################################

Expand Down Expand Up @@ -88,7 +91,7 @@ HARD_RESTART_DELAY=$(( 60 * 5 )) # (300 = 5 minutes)

################################## FUNCTIONS ##################################

# This function takes a number of seconds as input and returns hh:mm:ss format.
# Takes a number of seconds as input and returns hh:mm:ss format.
# Source: http://stackoverflow.com/a/12199798
# License: CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0/)
# Created by: perreal (http://stackoverflow.com/users/390913/perreal)
Expand All @@ -107,8 +110,8 @@ convert_seconds () {

}

# This function caches all available critical system updates, or exits if no
# critical updates are available.
# Caches all available critical system updates, or exits if no critical updates
# are available.
check_for_updates () {

echo "Checking for pending system updates..."
Expand Down Expand Up @@ -143,20 +146,19 @@ check_for_updates () {

}

# Invoked after the deferral deadline passes, this function displays an
# onscreen message instructing the user to apply updates.
# Displays an onscreen message instructing the user to apply updates.
# This function is invoked after the deferral deadline passes.
display_act_msg () {

# Create a jamfHelper script that will be called by a LaunchDaemon.
cat << EOF > "/private/tmp/$HELPER_SCRIPT"
cat << EOF > "$HELPER_SCRIPT"
#!/bin/bash
"$JAMFHELPER" -windowType "utility" -windowPosition "ur" -icon "$LOGO" -title "$MSG_ACT_HEADING" -description "$MSG_ACT"
EOF
chmod +x "/private/tmp/$HELPER_SCRIPT"
chmod +x "$HELPER_SCRIPT"

# Create the LaunchDaemon that we'll use to show the persistent jamfHelper
# messages.
HELPER_LD="/private/tmp/${BUNDLE_ID}_helper.plist"
cat << EOF > "$HELPER_LD"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Expand All @@ -167,7 +169,7 @@ EOF
<key>Label</key>
<string>${BUNDLE_ID}_helper</string>
<key>Program</key>
<string>/private/tmp/$HELPER_SCRIPT</string>
<string>$HELPER_SCRIPT</string>
<key>ThrottleInterval</key>
<integer>10</integer>
</dict>
Expand Down Expand Up @@ -195,7 +197,6 @@ run_updates () {
"$JAMFHELPER" -windowType "hud" -windowPosition "ur" -icon "$LOGO" -title "$MSG_UPDATING_HEADING" -description "$MSG_UPDATING" -lockHUD &
UPDATE_OUTPUT_CAPTURE="$(softwareupdate --install --$INSTALL_WHICH --no-scan 2>&1)"
echo "Finished running updates."
killall jamfHelper 2>/dev/null
clean_up

# Trigger restart if script ran an update which requires it.
Expand All @@ -211,24 +212,45 @@ run_updates () {

}

# Clean up plist values and self destruct LaunchDaemon and script.
# Initializes plist values and moves all script and LaunchDaemon resources to
# /private/tmp for deletion on a subsequent restart.
clean_up () {

echo "Killing any active jamfHelper notifications..."
killall jamfHelper 2>/dev/null

echo "Cleaning up stored plist values..."
defaults delete "$PLIST" AppleSoftwareUpdatesForcedAfter 2>/dev/null
defaults delete "$PLIST" AppleSoftwareUpdatesDeferredUntil 2>/dev/null

echo "Cleaning up main script and LaunchDaemons..."
mv "/Library/LaunchDaemons/$BUNDLE_ID.plist" "/private/tmp/$BUNDLE_ID.plist"
launchctl unload -w "$HELPER_LD"
mv "$0" "/private/tmp/"
echo "Cleaning up script resources..."
CLEANUP_FILES=(
"/Library/LaunchDaemons/$BUNDLE_ID.plist"
"$HELPER_LD"
"$HELPER_SCRIPT"
"$SCRIPT_PATH"
)
CLEANUP_DIR="/private/tmp/install-or-defer"
mkdir "$CLEANUP_DIR"
for TARGET_FILE in "${CLEANUP_FILES[@]}"; do
if [[ -e "$TARGET_FILE" ]]; then
mv -v "$TARGET_FILE" "$CLEANUP_DIR"
fi
done
if [[ $(launchctl list) =~ "${BUNDLE_ID}_helper" ]]; then
echo "Unloading ${BUNDLE_ID}_helper LaunchDaemon..."
launchctl remove "${BUNDLE_ID}_helper"
fi

}

# This function immediately attempts a "soft" restart, waits a specified amount
# of time, and then forces a "hard" restart.
# Restarts or shuts down the system depending on parameter input. Attempts a
# "soft" restart, waits a specified amount of time, and then forces a "hard"
# restart.
trigger_restart () {

clean_up

# Immediately attempt a "soft" restart.
echo "Attempting a \"soft\" $1..."
CURRENT_USER=$(/usr/bin/stat -f%Su /dev/console)
Expand Down Expand Up @@ -262,11 +284,12 @@ exit_without_updating () {

clean_up

if [[ -e "/private/tmp/$BUNDLE_ID.plist" ]]; then
"/bin/echo" "Unloading $BUNDLE_ID LaunchDaemon..."
"/bin/launchctl" unload -w "/private/tmp/$BUNDLE_ID.plist"
# Unload main LaunchDaemon. This will likely kill the script.
if [[ $(launchctl list) =~ "$BUNDLE_ID" ]]; then
echo "Unloading $BUNDLE_ID LaunchDaemon..."
launchctl remove "$BUNDLE_ID"
fi
"/bin/echo" "Script will end here."
echo "Script will end here."
exit 0

}
Expand All @@ -281,8 +304,9 @@ echo "Starting $(basename "$0") script. Performing validation and error checking
# Define custom $PATH.
PATH="/usr/sbin:/usr/bin:/usr/local/bin:$PATH"

# Filename we will use for the auto-generated helper script.
HELPER_SCRIPT="$(basename "$0" | sed "s/.sh$//g")_helper.sh"
# Filename and path we will use for the auto-generated helper script and LaunchDaemon.
HELPER_SCRIPT="/Library/Scripts/$(basename "$0" | sed "s/.sh$//g")_helper.sh"
HELPER_LD="/Library/LaunchDaemons/${BUNDLE_ID}_helper.plist"

# Flag variable for catching show-stopping errors.
BAILOUT=false
Expand Down

0 comments on commit 90af1a6

Please sign in to comment.