How to automatically make Mac shutdown after Time Machine Backup

How can I make my Macbook automatically shutdown after a Time Machine backup?

I have a 40GB worth backup to do but I keep having to cancel it to go to bed. Would be great if I could leave it on to do it overnight.

Any suggestions of how to do this in ML?


Solution 1:

You could setup a crontab to use tmutil through the command line to do a backup and then shutdown. since shutdown requires sudo privs, you have to set your crontab up as sudo.

sudo crontab -e

Enter your password and then that brings you to edit the contab file

shutdown -h now

Shutdown does exactly that but with the -h flag, it halts the system (shutdown). For the shutdown to work, this would have to be setup as an crontab under sudo

tmutil startbackup

Starts the timemachine backup.

Putting it all together:

00 20 ** ** ** tmutil startbackup --block && shutdown -h +5

Would do the backup at 10:00pm every day and then shutdown

Solution 2:

I wanted to be able to leave an existing backup to complete, so my approach is a little different.

The backupd process seems only to be running whilst a backup is underway (and for a minute or two afterwards, from what I've observed) so I logged in as root (so that I can (a) see all users' process and (b) trigger a shutdown) and then polled for the process to finish before shutting down. So:

sudo -s

and enter your password to become root, then:

while ( ps -ef | grep 'backupd$' ); do echo "Still backing up..."; sleep 60; done; shutdown -h +1

to wait for backupd to finish and then shut down. (I used shutdown -h +1 instead of shutdown -h now just to be over-cautious and give it an extra minute to tidy up or whatever before shutting down.)

Solution 3:

Assuming you have already set a destination for your TM, you could open a shell in Terminal (or ssh in), sudo su into super-user mode, then run tmutil startbackup --block;shutdown -h now;exit which tells TM to begin a backup immediately, turn off the Mac, and exit the shell. man tmutil for more info.

You could also then add this as a launchctl item or even cron it to schedule.