Im looking to create a script to add to a new policy. I would like the script to prompt the user to restart their machine after the policy runs. I would also like the user to be able to have the option to restart the machine now or later. in other words, I don't want their machines to restart without the users permission. This could cause a ton of trouble with information loss etc. I have no experience in scripting at all. I could somewhat follow some things. I am unable to right scripts on my own at this point. Would be very helpful if someone can assist me.
12 replies

- Valued Contributor
- 95 replies
- February 27, 2018
What is it you are needing to change/install that requires a restart. Not everything in macOS actually needs a restart. Maybe start there?

- Author
- Contributor
- 79 replies
- February 27, 2018
Im having an issue with Jamf Remote. I cant control any machines without resetting our management account password. i created a policy to reset the passwords. The policy seems to only work half of the time. I also realized that it wont work without me restarting the machine. I wanted to create a script in the policy that prompts the user to restart the machine after it runs.

- Valued Contributor
- 193 replies
- February 27, 2018
I have a two script process (modified from a jamfHelper delay script) that presents a jamfHelper popup if the computer has not restarted in 4 days. It gives the user the option of an immediate restart, 2 minute, 5 minute or 1 hour delay. It then presents another jamfHelper popup 1 min before restart. The second script (for clean up) runs at login and checks for the presence of the files and removes them if they are there.
Script One:
1#!/bin/bash23#######################################################################################4#5# Copyright (c) 2016, JAMF Software, LLC. All rights reserved.6#7# Redistribution and use in source and binary forms, with or without8# modification, are permitted provided that the following conditions are met:9# * Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# * Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14# * Neither the name of the JAMF Software, LLC nor the15# names of its contributors may be used to endorse or promote products16# derived from this software without specific prior written permission.17#18# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY19# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE21# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND25# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS27# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28#29#######################################################################################30#31#######################################################################################32#33# Rewrite Description34#35#######################################################################################36# jss.jhp.delay.sh37# ©2016 brock walters jamf38#39# Modified for KISD by Mike Donovan September 8, 201740#41# the information in the jamfhelper pop-up window can be modified by changing the following below:42#43# -title44# -heading45# -description46# -icon (eg, a .b64 encoded .png or .icns file in the script or a reference to a graphics file)47# -button1 (limited characters in field)48# -button2 (limited characters in field)49# -showDelayOptions (in seconds)50#51# for other jamfHelper options see:52# 53# /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -help54#55# the case statement takes input from the jamfHelper button exit code variable "$result"56#5758lastBootRaw=$(sysctl kern.boottime | awk -F'[= |,]' '{print $6}')5960lastBootFormat=$(date -jf "%s" "$lastBootRaw" +"%m-%d-%Y")616263today=$(date +%s)64#today=$(date -v+4d +%s) ###########For Testing #############################################6566diffDays=$(( (today - lastBootRaw) / 86400 ))6768#echo $diffDays6970if [ $diffDays -ge 4 ];then7172 #echo "4 days or more Running Reboot script"73 # Check for district icons74 file=$(find /Library/Application Support/JAMF/bin/KISDColorseal.png)75 if [ ! -z "$file" ]76 then77 useIcon=/Library/Application Support/JAMF/bin/KISDColorseal.png78 #echo "found"79 else80 useIcon=/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertCautionIcon.icns81 #echo "not found"82 fi8384else85 #echo "3 days or less Exiting"86 exit 087fi8889jamfhelper()90{91/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper 92-windowType utility 93-title "KISD Mac Systems Admin" 94-heading "It's time to reboot" 95-description "This computer has not restarted since $lastBootFormat. Restart now or choose a delay option." 96-icon "$useIcon" 97-iconSize 110 98-button1 "Delay" 99-button2 "Restart Now" 100-showDelayOptions "120, 300, 3600" # 2 minutes, 5 minutes, 1 hour101}102103# variables104result=$(jamfhelper)105delayint=$(echo "$result" | /usr/bin/sed 's/.$//')106warndelayint=$(expr $delayint - 60)107#echo $delayint108#echo $warndelayint109defercal=$(($(/bin/date +%s) + delayint))110hour=$(/bin/date -j -f "%s" "$defercal" "+%H")111minute=$(/bin/date -j -f "%s" "$defercal" "+%M")112#echo $hour113#echo $minute114warndefercal=$(($(/bin/date +%s) + warndelayint))115warnhour=$(/bin/date -j -f "%s" "$warndefercal" "+%H")116warnminute=$(/bin/date -j -f "%s" "$warndefercal" "+%M")117#echo $warnhour118#echo $warnminute119120# write launch daemon populated with variables from jamfHelper output121122delay()123{124/bin/cat <<EOF > /Library/LaunchDaemons/org.your.rebootdelay.plist125<?xml version="1.0" encoding="UTF-8"?>126<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"127 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">128<plist version="1.0">129<dict>130 <key>Label</key>131 <string>org.your.rebootdelay</string>132 <key>ProgramArguments</key>133 <array>134 <string>reboot</string>135 </array>136 <key>StartCalendarInterval</key>137 <dict>138 <key>Hour</key>139 <integer>$hour</integer>140 <key>Minute</key>141 <integer>$minute</integer>142 </dict>143</dict>144</plist>145EOF146}147148warndelay()149{150/bin/cat <<EOF > /Library/LaunchDaemons/org.your.rebootdelaywarning.plist151<?xml version="1.0" encoding="UTF-8"?>152<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"153 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">154<plist version="1.0">155<dict>156 <key>Label</key>157 <string>org.your.rebootdelaywarning</string>158 <key>ProgramArguments</key>159 <array>160 <string>sh</string>161 <string>/Library/Scripts/rebootwarning.sh</string>162 </array>163 <key>StartCalendarInterval</key>164 <dict>165 <key>Hour</key>166 <integer>$warnhour</integer>167 <key>Minute</key>168 <integer>$warnminute</integer>169 </dict>170</dict>171</plist>172EOF173}174175warnScript()176{177/bin/cat <<EOF > /Library/Scripts/rebootwarning.sh178#!/bin/bash179180/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper 181-windowType utility 182-title "KISD Systems Admin" 183-heading "It's time to reboot" 184-description "This computer is set to reboot in 1 minute." 185-icon "$useIcon" 186-iconSize 110 187-button1 "Ok" 188189EOF190}191192finalPrep()193{194# unload launchd for testing195#launchctl unload /Library/LaunchDaemons/org.your.rebootdelay.plist196#launchctl unload /Library/LaunchDaemons/org.your.rebootdelaywarning.plist197198# set ownership on delay launch daemon199chown root:wheel /Library/LaunchDaemons/org.your.rebootdelay.plist200chmod 644 /Library/LaunchDaemons/org.your.rebootdelay.plist201202# set ownership on delaywarning launch daemon203chown root:wheel /Library/LaunchDaemons/org.your.rebootdelaywarning.plist204chmod 644 /Library/LaunchDaemons/org.your.rebootdelaywarning.plist205206#load launchd207launchctl load /Library/LaunchDaemons/org.your.rebootdelay.plist208launchctl load /Library/LaunchDaemons/org.your.rebootdelaywarning.plist209210}211212# select action based on user input213#### Script Execution Starts Here #######214#### 1 runs functions215#### 2 runs reboot command216case "$result" in217 *1 ) delay218 warndelay219 warnScript220 finalPrep221 ;;222 *2 ) reboot223 echo "Reboot Called"224 ;;225esac226227exit 0```228229Script two:
!/bin/bash
file=$(find /Library/LaunchDaemons/org.your.rebootdelay.plist)
if [ ! -z "$file" ]
then
echo "Found"
rm /Library/LaunchDaemons/org.your.rebootdelay.plist
rm /Library/LaunchDaemons/org.your.rebootdelaywarning.plist
rm /Library/Scripts/rebootwarning.sh
launchctl remove org.your.rebootdelay
launchctl remove org.your.rebootdelaywarning
fi
exit 0
```

- Author
- Contributor
- 79 replies
- February 27, 2018
I need one that I can attach to specific policies. I would like it to inform the user that the computer is going to restart/has to restart. I want the user to be able choose if he would like to restart now or later.
- Legendary Contributor
- 7886 replies
- February 27, 2018
@kadams I think the script posted above can get you there, but will obviously need some tweaking for your purposes.
If you want, you can also take a look at something I put together a while ago now that uses a similar approach, but was intended more to be run after critical updates were installed on a device. reboot_scheduler

- Author
- Contributor
- 79 replies
- March 8, 2018
@m.donovan , I tried putting this script into a text editor. I cant get it to run in terminal. Is there anything else I supposed to change for this to work?. Am I supposed to put both of these scripts in the text editor at once?

- Author
- Contributor
- 79 replies
- April 11, 2018
Anyone knows how to modify the script above. I need it to trigger after running a policy. For instance, one policy runs and the script triggers. 4 days is too much time. I would like it to run instantly after running a policy. Also can both of those scripts be combined? Do I need them both for the script tor run.

- Valued Contributor
- 142 replies
- April 12, 2018
I think the original script Brock posted may be closer to what you have in mind.
I would be sure to test it thoroughly for your needs.

- Valued Contributor
- 391 replies
- April 12, 2018
Am I missing something??
Why not just use the 'allow deferral' in the user interaction tab of the policy??

- Author
- Contributor
- 79 replies
- April 12, 2018
I used the script above, but the delay portion of it isnt working. I delay for 2 minutes and nothing happens. Also is script 2 combined into one script?

- Valued Contributor
- 142 replies
- April 12, 2018
I'm not sure if your reply was directed at me but the script I linked to is meant to only be 1 script. It also doesnt have logic built into it about last reboot time.
Additionally regarding the script you tried I noticed the script has logic based on the last reboot time. Which means if the system you tested on was rebooted less than 4 days ago you may not see any results of the above script.
Im looking to create a script to add to a new policy. I would like the script to prompt the user to restart their machine after the policy runs. I would also like the user to be able to have the option to restart the machine now or later. in other words, I don't want their machines to restart without the users permission. This could cause a ton of trouble with information loss etc. I have no experience in scripting at all. I could somewhat follow some things. I am unable to right scripts on my own at this point. Would be very helpful if someone can assist me.
What is it you are needing to change/install that requires a restart. Not everything in macOS actually needs a restart. Maybe start there?
Im having an issue with Jamf Remote. I cant control any machines without resetting our management account password. i created a policy to reset the passwords. The policy seems to only work half of the time. I also realized that it wont work without me restarting the machine. I wanted to create a script in the policy that prompts the user to restart the machine after it runs.
I have a two script process (modified from a jamfHelper delay script) that presents a jamfHelper popup if the computer has not restarted in 4 days. It gives the user the option of an immediate restart, 2 minute, 5 minute or 1 hour delay. It then presents another jamfHelper popup 1 min before restart. The second script (for clean up) runs at login and checks for the presence of the files and removes them if they are there.
Script One:
1#!/bin/bash23#######################################################################################4#5# Copyright (c) 2016, JAMF Software, LLC. All rights reserved.6#7# Redistribution and use in source and binary forms, with or without8# modification, are permitted provided that the following conditions are met:9# * Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# * Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14# * Neither the name of the JAMF Software, LLC nor the15# names of its contributors may be used to endorse or promote products16# derived from this software without specific prior written permission.17#18# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY19# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE21# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND25# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS27# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28#29#######################################################################################30#31#######################################################################################32#33# Rewrite Description34#35#######################################################################################36# jss.jhp.delay.sh37# ©2016 brock walters jamf38#39# Modified for KISD by Mike Donovan September 8, 201740#41# the information in the jamfhelper pop-up window can be modified by changing the following below:42#43# -title44# -heading45# -description46# -icon (eg, a .b64 encoded .png or .icns file in the script or a reference to a graphics file)47# -button1 (limited characters in field)48# -button2 (limited characters in field)49# -showDelayOptions (in seconds)50#51# for other jamfHelper options see:52# 53# /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -help54#55# the case statement takes input from the jamfHelper button exit code variable "$result"56#5758lastBootRaw=$(sysctl kern.boottime | awk -F'[= |,]' '{print $6}')5960lastBootFormat=$(date -jf "%s" "$lastBootRaw" +"%m-%d-%Y")616263today=$(date +%s)64#today=$(date -v+4d +%s) ###########For Testing #############################################6566diffDays=$(( (today - lastBootRaw) / 86400 ))6768#echo $diffDays6970if [ $diffDays -ge 4 ];then7172 #echo "4 days or more Running Reboot script"73 # Check for district icons74 file=$(find /Library/Application Support/JAMF/bin/KISDColorseal.png)75 if [ ! -z "$file" ]76 then77 useIcon=/Library/Application Support/JAMF/bin/KISDColorseal.png78 #echo "found"79 else80 useIcon=/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertCautionIcon.icns81 #echo "not found"82 fi8384else85 #echo "3 days or less Exiting"86 exit 087fi8889jamfhelper()90{91/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper 92-windowType utility 93-title "KISD Mac Systems Admin" 94-heading "It's time to reboot" 95-description "This computer has not restarted since $lastBootFormat. Restart now or choose a delay option." 96-icon "$useIcon" 97-iconSize 110 98-button1 "Delay" 99-button2 "Restart Now" 100-showDelayOptions "120, 300, 3600" # 2 minutes, 5 minutes, 1 hour101}102103# variables104result=$(jamfhelper)105delayint=$(echo "$result" | /usr/bin/sed 's/.$//')106warndelayint=$(expr $delayint - 60)107#echo $delayint108#echo $warndelayint109defercal=$(($(/bin/date +%s) + delayint))110hour=$(/bin/date -j -f "%s" "$defercal" "+%H")111minute=$(/bin/date -j -f "%s" "$defercal" "+%M")112#echo $hour113#echo $minute114warndefercal=$(($(/bin/date +%s) + warndelayint))115warnhour=$(/bin/date -j -f "%s" "$warndefercal" "+%H")116warnminute=$(/bin/date -j -f "%s" "$warndefercal" "+%M")117#echo $warnhour118#echo $warnminute119120# write launch daemon populated with variables from jamfHelper output121122delay()123{124/bin/cat <<EOF > /Library/LaunchDaemons/org.your.rebootdelay.plist125<?xml version="1.0" encoding="UTF-8"?>126<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"127 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">128<plist version="1.0">129<dict>130 <key>Label</key>131 <string>org.your.rebootdelay</string>132 <key>ProgramArguments</key>133 <array>134 <string>reboot</string>135 </array>136 <key>StartCalendarInterval</key>137 <dict>138 <key>Hour</key>139 <integer>$hour</integer>140 <key>Minute</key>141 <integer>$minute</integer>142 </dict>143</dict>144</plist>145EOF146}147148warndelay()149{150/bin/cat <<EOF > /Library/LaunchDaemons/org.your.rebootdelaywarning.plist151<?xml version="1.0" encoding="UTF-8"?>152<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"153 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">154<plist version="1.0">155<dict>156 <key>Label</key>157 <string>org.your.rebootdelaywarning</string>158 <key>ProgramArguments</key>159 <array>160 <string>sh</string>161 <string>/Library/Scripts/rebootwarning.sh</string>162 </array>163 <key>StartCalendarInterval</key>164 <dict>165 <key>Hour</key>166 <integer>$warnhour</integer>167 <key>Minute</key>168 <integer>$warnminute</integer>169 </dict>170</dict>171</plist>172EOF173}174175warnScript()176{177/bin/cat <<EOF > /Library/Scripts/rebootwarning.sh178#!/bin/bash179180/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper 181-windowType utility 182-title "KISD Systems Admin" 183-heading "It's time to reboot" 184-description "This computer is set to reboot in 1 minute." 185-icon "$useIcon" 186-iconSize 110 187-button1 "Ok" 188189EOF190}191192finalPrep()193{194# unload launchd for testing195#launchctl unload /Library/LaunchDaemons/org.your.rebootdelay.plist196#launchctl unload /Library/LaunchDaemons/org.your.rebootdelaywarning.plist197198# set ownership on delay launch daemon199chown root:wheel /Library/LaunchDaemons/org.your.rebootdelay.plist200chmod 644 /Library/LaunchDaemons/org.your.rebootdelay.plist201202# set ownership on delaywarning launch daemon203chown root:wheel /Library/LaunchDaemons/org.your.rebootdelaywarning.plist204chmod 644 /Library/LaunchDaemons/org.your.rebootdelaywarning.plist205206#load launchd207launchctl load /Library/LaunchDaemons/org.your.rebootdelay.plist208launchctl load /Library/LaunchDaemons/org.your.rebootdelaywarning.plist209210}211212# select action based on user input213#### Script Execution Starts Here #######214#### 1 runs functions215#### 2 runs reboot command216case "$result" in217 *1 ) delay218 warndelay219 warnScript220 finalPrep221 ;;222 *2 ) reboot223 echo "Reboot Called"224 ;;225esac226227exit 0```228229Script two:
!/bin/bash
file=$(find /Library/LaunchDaemons/org.your.rebootdelay.plist)
if [ ! -z "$file" ]
then
echo "Found"
rm /Library/LaunchDaemons/org.your.rebootdelay.plist
rm /Library/LaunchDaemons/org.your.rebootdelaywarning.plist
rm /Library/Scripts/rebootwarning.sh
launchctl remove org.your.rebootdelay
launchctl remove org.your.rebootdelaywarning
fi
exit 0
```
I need one that I can attach to specific policies. I would like it to inform the user that the computer is going to restart/has to restart. I want the user to be able choose if he would like to restart now or later.
@kadams I think the script posted above can get you there, but will obviously need some tweaking for your purposes.
If you want, you can also take a look at something I put together a while ago now that uses a similar approach, but was intended more to be run after critical updates were installed on a device. reboot_scheduler
@m.donovan , I tried putting this script into a text editor. I cant get it to run in terminal. Is there anything else I supposed to change for this to work?. Am I supposed to put both of these scripts in the text editor at once?
Anyone knows how to modify the script above. I need it to trigger after running a policy. For instance, one policy runs and the script triggers. 4 days is too much time. I would like it to run instantly after running a policy. Also can both of those scripts be combined? Do I need them both for the script tor run.
I think the original script Brock posted may be closer to what you have in mind.
I would be sure to test it thoroughly for your needs.
Am I missing something??
Why not just use the 'allow deferral' in the user interaction tab of the policy??
I used the script above, but the delay portion of it isnt working. I delay for 2 minutes and nothing happens. Also is script 2 combined into one script?
I'm not sure if your reply was directed at me but the script I linked to is meant to only be 1 script. It also doesnt have logic built into it about last reboot time.
Additionally regarding the script you tried I noticed the script has logic based on the last reboot time. Which means if the system you tested on was rebooted less than 4 days ago you may not see any results of the above script.
Thanks
Reply
Related topics
Configure Platform Single Sign-On for Okta Identity Engine with Jamf Proicon
General DiscussionsMounting SMB Network Shares Using Jamf Configuration Profilesicon
General DiscussionsConfigure Platform Single Sign-On (PSSOe) for Microsoft Entra ID with Jamf Proicon
General DiscussionsCustom Schemas For Application Configuration Profilesicon
General DiscussionsNew Device sign-in with SSO, Jamf Connect (Azure) and auto device naming based on the users infoicon
General Discussions
Most helpful members this week
- Chubs
58 likes
- tommypatzius
54 likes
- MusicCityMac
28 likes
- ktrojano
26 likes
- GregBobbett
26 likes
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
Scanning file for viruses.
Sorry, we're still checking this file's contents to make sure it's safe to download. Please try again in a few minutes.
OKThis file cannot be downloaded
Sorry, our virus scanner detected that this file isn't safe to download.
OKCookie policy
We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.
Cookie settings
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.