Skip to main content
Question

Scripting


Forum|alt.badge.img+6

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

Forum|alt.badge.img+15
  • 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?


Forum|alt.badge.img+6
  • 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.


Forum|alt.badge.img+10
  • 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/bash
2
3#######################################################################################
4#
5# Copyright (c) 2016, JAMF Software, LLC. All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14# * Neither the name of the JAMF Software, LLC nor the
15# names of its contributors may be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
19# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29#######################################################################################
30#
31#######################################################################################
32#
33# Rewrite Description
34#
35#######################################################################################
36# jss.jhp.delay.sh
37# ©2016 brock walters jamf
38#
39# Modified for KISD by Mike Donovan September 8, 2017
40#
41# the information in the jamfhelper pop-up window can be modified by changing the following below:
42#
43# -title
44# -heading
45# -description
46# -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 -help
54#
55# the case statement takes input from the jamfHelper button exit code variable "$result"
56#
57
58lastBootRaw=$(sysctl kern.boottime | awk -F'[= |,]' '{print $6}')
59
60lastBootFormat=$(date -jf "%s" "$lastBootRaw" +"%m-%d-%Y")
61
62
63today=$(date +%s)
64#today=$(date -v+4d +%s) ###########For Testing #############################################
65
66diffDays=$(( (today - lastBootRaw) / 86400 ))
67
68#echo $diffDays
69
70if [ $diffDays -ge 4 ];then
71
72 #echo "4 days or more Running Reboot script"
73 # Check for district icons
74 file=$(find /Library/Application Support/JAMF/bin/KISDColorseal.png)
75 if [ ! -z "$file" ]
76 then
77 useIcon=/Library/Application Support/JAMF/bin/KISDColorseal.png
78 #echo "found"
79 else
80 useIcon=/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertCautionIcon.icns
81 #echo "not found"
82 fi
83
84else
85 #echo "3 days or less Exiting"
86 exit 0
87fi
88
89jamfhelper()
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 hour
101}
102
103# variables
104result=$(jamfhelper)
105delayint=$(echo "$result" | /usr/bin/sed 's/.$//')
106warndelayint=$(expr $delayint - 60)
107#echo $delayint
108#echo $warndelayint
109defercal=$(($(/bin/date +%s) + delayint))
110hour=$(/bin/date -j -f "%s" "$defercal" "+%H")
111minute=$(/bin/date -j -f "%s" "$defercal" "+%M")
112#echo $hour
113#echo $minute
114warndefercal=$(($(/bin/date +%s) + warndelayint))
115warnhour=$(/bin/date -j -f "%s" "$warndefercal" "+%H")
116warnminute=$(/bin/date -j -f "%s" "$warndefercal" "+%M")
117#echo $warnhour
118#echo $warnminute
119
120# write launch daemon populated with variables from jamfHelper output
121
122delay()
123{
124/bin/cat <<EOF > /Library/LaunchDaemons/org.your.rebootdelay.plist
125<?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>
145EOF
146}
147
148warndelay()
149{
150/bin/cat <<EOF > /Library/LaunchDaemons/org.your.rebootdelaywarning.plist
151<?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>
172EOF
173}
174
175warnScript()
176{
177/bin/cat <<EOF > /Library/Scripts/rebootwarning.sh
178#!/bin/bash
179
180/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"
188
189EOF
190}
191
192finalPrep()
193{
194# unload launchd for testing
195#launchctl unload /Library/LaunchDaemons/org.your.rebootdelay.plist
196#launchctl unload /Library/LaunchDaemons/org.your.rebootdelaywarning.plist
197
198# set ownership on delay launch daemon
199chown root:wheel /Library/LaunchDaemons/org.your.rebootdelay.plist
200chmod 644 /Library/LaunchDaemons/org.your.rebootdelay.plist
201
202# set ownership on delaywarning launch daemon
203chown root:wheel /Library/LaunchDaemons/org.your.rebootdelaywarning.plist
204chmod 644 /Library/LaunchDaemons/org.your.rebootdelaywarning.plist
205
206#load launchd
207launchctl load /Library/LaunchDaemons/org.your.rebootdelay.plist
208launchctl load /Library/LaunchDaemons/org.your.rebootdelaywarning.plist
209
210}
211
212# select action based on user input
213#### Script Execution Starts Here #######
214#### 1 runs functions
215#### 2 runs reboot command
216case "$result" in
217 *1 ) delay
218 warndelay
219 warnScript
220 finalPrep
221 ;;
222 *2 ) reboot
223 echo "Reboot Called"
224 ;;
225esac
226
227exit 0```
228
229Script 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
```


Forum|alt.badge.img+6
  • 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.


mm2270
Forum|alt.badge.img+24
  • 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


Forum|alt.badge.img+6
  • 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?


Forum|alt.badge.img+6
  • 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.


Forum|alt.badge.img+11
  • Valued Contributor
  • 142 replies
  • April 12, 2018

@kadams

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.


Forum|alt.badge.img+13
  • 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??


Forum|alt.badge.img+6
  • 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?


Forum|alt.badge.img+11
  • Valued Contributor
  • 142 replies
  • April 12, 2018

@kadams

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.


Forum|alt.badge.img+6
  • Author
  • Contributor
  • 79 replies
  • April 12, 2018

Thanks


Reply


Cookie 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