Shut down automatically after time machine backup

With this, you’ll find a script that I made to shut down my mac automatically after a time machine backup. This script works fine for my old mac pro (early 2009) which runs on osx el Capitan. I made this script some years ago by trial and error from examples and ideas I found on the net.

My problem is that this script doesn’t work on macs which run on newer versions of osx. It doesn’t run for instance on my MacBook Air that runs on osx Mojave. I would like to use this script on my MacBook air also. Over time I got attached to the comfort of mak-ing a time machine backup at the end of a working day and leaving the task of shutting down to the system itself.

Can you help me to update this script for use on osx Mojave?



property querylist : {"backupd"}

set app_is_up to false

do shell script "tmutil startbackup"

repeat
	delay 10
	repeat with i in querylist
		if IsProcRunning(i) then set app_is_up to true
	end repeat
	if app_is_up then
		set app_is_up to false
	else
		ignoring application responses
			tell application "loginwindow" to «event aevtshut»
		end ignoring
		exit repeat
	end if
end repeat

on IsProcRunning(theProc)
	try
		do shell script "ps auxc | grep \"" & theProc & "\""
		return true
	on error
		return false
	end try
end IsProcRunning


Model: MacBook Air
AppleScript: 2.7
Browser: Safari 606.4.5
Operating System: macOS 10.14

As you are using tmutil anyway this is an alternative. It monitors the Running value of the result of tmutil status

Save the code as application


on run
	do shell script "tmutil startbackup"
	delay 30
end run

on idle
	if (do shell script "tmutil status | awk '/Running/ {print $NF}'") starts with "1" then
		return 5.0
	else
		ignoring application responses
			tell application "loginwindow" to «event aevtshut»
		end ignoring
		quit
	end if
end idle

Thank you both for responding with your suggestions! I tried both on my MacBook Air.

The first one (#2) activated Time Machine, but without starting a backup.

I exported the second one (#3) to an app, which starts indeed a backup but ends itself even before the backup is completed. Therefor it doesn’t run long enough to provide in a shut down of the computer.

Thanks so far and hoping you can help me out,
Regards,
George

I have never used Time Machine before, but as I understand from what I read on the network, in order to make a backup manually, I first need to set off the automatic backup. And, perhaps, this will help you:

do shell script "/usr/bin/tmutil startbackup --auto --block"

Option –block gives you “Wait until the backup is finished before exiting” behavior.
The –auto option provides a supported mechanism with which to
trigger “automatic-like” backups, similar to automatic backups
that are scheduled by the system. While this is not identical to
true system-scheduled backups, it provides custom schedulers the
ability to achieve some (but not all) behavior normally exhibited
when operating in automatic mode.

Here you can check backup completing in this way:


set shellcommandExitCode to do shell script "/usr/bin/tmutil startbackup --auto --block"
if shellcommandExitCode = 0 then
 backup_Was_SucessFull()
  shutDown_MyMac()
end if 

But why do you need this script? If you want to set up Time Machine for 1 time per day, then you just need to do this in the terminal:

sudo defaults write /System/Library/LaunchDaemons/com.apple.backupd-auto StartInterval -int 86400

and in System Preferences for Time Machine set AUTO backups. Then, you can change your script to check completing Time Machine job and shutdown your Mac

Success!

Dear KniasidisR and StefanK,

thanks to your suggestions I found a script that works just as I intended. It appeared to be a lot simpler than I expected. Here it is:



do shell script "/usr/bin/tmutil startbackup --block"

delay 10

tell application "System Events"
	shut down
end tell


The reason why I like to use this script is, that I use some Virtual Machines. Those VM’s leave big files on my computer, and backing up those files takes time. It is convenient for me to start a TM backup at the end of a working period and to leave the computer starting this task, knowing that it will shut itself down after completion.

Regards,
GeorgeQ