Scheduling MacOS Updates

Leal
New Contributor III

For sometime now I've had the need to schedule update like we do for our Windows Systems on the second Thursday of the month. Time after time JAMF has told that I need to create a feature request so after hoping update after update I decided to create a python script that will do this for me. Here is the breakdown on how I managed to get it done.

You need to create Smart Group that's based on machines with available updates and limited by network (not required)
POLICY NOTE
- Each policy is set to run daily and is limited to run on Thursdays only.
- The only difference between script 1/2/3 are the commands that are run. - In our setup each policy is giving a one hour time windows. This might be a little too much but it has worked since it start at 12:00 am

-------------------------------------------Below are the individual policy order that run the scripts ---------------------

  1. Restart mac.
  2. run update script
  3. update client Inventory. Fail safe in case that an update becomes available after the previous updates were install.
  4. run update script

Main update script. Which was taken from sources online and worked on by some folks in our group. NOTE the main script will check and see if the updates require a restart and will do so if need. If not it will just install the update.

Below is the script used in step 2 and 4

#!/usr/bin/python
# nthday.py
#worked on by Isaac leal and Ed Fast.
# taken from here and modified  http://code.activestate.com/recipes/425607-findng-the-xth-day-in-a-month/

from calendar import monthrange
import datetime
import subprocess



FIRST = 0
SECOND = 1
THIRD = 2
FOURTH = FORTH = 3  # for people who have finger trouble
FIFTH = 4
LAST = -1
SECONDLAST = -2
THIRDLAST = -3

MONDAY = MON = 0
TUESDAY = TUE = TUES = 1
WEDNESDAY = WED = 2
THURSDAY = THU = THUR = 3
FRIDAY = FRI = 4
SATURDAY = SAT = 5
SUNDAY = SUN = 6

JANUARY = JAN = 1
FEBRUARY = FEB = 2
MARCH = MAR = 3
APRIL = APR = 4
MAY = 5
JUNE = JUN = 6
JULY = JUL = 7
AUGUST = AUG = 8
SEPTEMBER = SEP = 9
OCTOBER = OCT = 10
NOVEMBER = NOV = 11
DECEMBER = DEC = 12


def dow_date_finder(which_weekday_in_month=FIRST,day=MONDAY,month=JANUARY,year=2017):
    bom, days = monthrange(year, month)
    firstmatch = (day - bom) % 7 + 1
    return xrange(firstmatch, days+1, 7)[which_weekday_in_month]



currentDate = datetime.datetime.now()

thisMonth2ndThur = dow_date_finder(which_weekday_in_month=SECOND, day=THUR, month=currentDate.month, year=currentDate.year)

print "{0}/{1}/{2}".format(currentDate.month, currentDate.day, currentDate.year)
print thisMonth2ndThur

if currentDate.day == thisMonth2ndThur:
    print "Today is the 2nd Thursday of the month - run the script"
    command = "/usr/sbin/softwareupdate -l"
    #command = "ls -la /usr"

    # https://docs.python.org/3/library/subprocess.html#subprocess.check_output
    output = subprocess.check_output(command.split())  # run the command with the supplied arguments.
    print output

    if "restart" in output:  # search for 'restart' in the output
        print "restart machine"
        subprocess.call(['/usr/sbin/softwareupdate', '-i', '-a'])
        subprocess.call(['/sbin/shutdown', '-r', 'now'])
    else:
        print "don't restart"
        subprocess.call(['/usr/sbin/softwareupdate', '-i', '-a'])
else:
    print "Today is not the 2nd Thursday of the month - exit"

Differences in scripts 1 and 3

1

if currentDate.day == thisMonth2ndThur:
    print "Today is the 2nd Thursday of the month - run the script"
    subprocess.call(['/sbin/shutdown', '-r', 'now'])
else:
    print "Today is not the 2nd Thursday of the month - exit"

3

if currentDate.day == thisMonth2ndThur:
    print "Today is the 2nd Thursday of the month - run the script"
    subprocess.call(['/sbin/shutdown', '-recon'])
else:
    print "Today is not the 2nd Thursday of the month - exit"

Hope this helps someone out there.

0 REPLIES 0