using Crontab for shutdown

rnapoles07
New Contributor

Hello,

I want to shut down a group of mac minis at 9 PM using a cronjob. I have this written, but not useful. Would anyone have any advice on how to implement a script for this?

command being used:

Sudo crontab -i
i
* 21 * * * /sbin/shutdown -h now
Esc
:wq

 

3 REPLIES 3

Hugonaut
Valued Contributor II

This is old but it should point you in the right direction, edit as necessary, the launchctl load command is different now. Good luck.

 

#!/bin/bash

# Create /Library/Scripts/shutdown.sh
sudo touch /Library/Scripts/shutdown.sh

echo > /Library/Scripts/shutdown.sh '#!/bin/bash
# Sleep for 60 seconds
sleep 60

# Shutdown Immediately
sudo shutdown -h now'

sudo chmod -x /Library/Scripts/shutdown.sh

# Create Launchdaemon

echo "<?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">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.restart.weekday</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Library/Scripts/shutdown.sh</string>
	</array>
	<key>StartCalendarInterval</key>
	<!--  Weekdays are 1 - 5; Sunday is 0 and 7   -->
		<array>
		<dict>
			<key>Weekday</key>
			<integer>1</integer>
			<key>Hour</key>
			<integer>21</integer>
			<key>Minute</key>
			<integer>00</integer>
		</dict>
		<dict>
			<key>Weekday</key>
			<integer>2</integer>
			<key>Hour</key>
			<integer>21</integer>
			<key>Minute</key>
			<integer>00</integer>
		</dict>
		<dict>
			<key>Weekday</key>
			<integer>3</integer>
			<key>Hour</key>
			<integer>21</integer>
			<key>Minute</key>
			<integer>00</integer>
		</dict>
		<dict>
			<key>Weekday</key>
			<integer>4</integer>
			<key>Hour</key>
			<integer>21</integer>
			<key>Minute</key>
			<integer>00</integer>
		</dict>
		<dict>
			<key>Weekday</key>
			<integer>5</integer>
			<key>Hour</key>
			<integer>21</integer>
			<key>Minute</key>
			<integer>00</integer>
		</dict>
	</array>
</dict>
</plist>" > /Library/LaunchDaemons/com.restart.weekday.plist

sudo chown root:wheel /Library/LaunchDaemons/com.restart.weekday.plist
sudo chmod 755 /Library/LaunchDaemons/com.restart.weekday.plist
sudo launchctl load /Library/LaunchDaemons/com.restart.weekday.plist

 

 

________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

sdagley
Esteemed Contributor II

@rnapoles07 The old school way to schedule a Mac to shutdown is to use the power management settings command pmset. To schedule a recurring shutdown every day at 9pm the command would be:

pmset repeat shutdown MTWRFSU 21:00:00

 Note that unlike 'shutdown -h now' a scheduled shutdown via power management settings won't shut down if any open app won't quit due to unsaved changes (or at least it used to work that way, it's been several versions of macOS since I've used this)

Ismere
Contributor

Hi everyone,
just a quick question, out of curiosity, is it important to use a cronjob for the shutdown?
I mean technically if the mac-minis used there are managed via Jamf you could always push out a Configuration Profil with the Energy Saver payload, including the Schedule Option to define a shutdown.

Beside that a daemon in the direction of what @Hugonaut suggested would be the more future proof approach in comparison to a cronjob.
If you are not used to writing daemons, using the application Lingon to create them is a good option.