Saturday, August 24, 2019

#1 2019-05-11 11:26:53 am

GeorgeQ
Member
Registered: 2019-05-07
Posts: 3

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?

Applescript:



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


Filed under: shut down, time machine

Offline

 

#2 2019-05-11 03:36:20 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 434

Re: Shut down automatically after time machine backup

Applescript:


property querylist : {"backupd"}

set app_is_up to false

do shell script "\"/Applications/Time Machine.app/Contents/MacOS/Time Machine\""

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

Last edited by KniazidisR (2019-05-11 03:41:10 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#3 2019-05-12 03:08:28 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11579
Website

Re: Shut down automatically after time machine backup

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

Applescript:


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


regards

Stefan

Offline

 

#4 2019-05-13 02:08:57 pm

GeorgeQ
Member
Registered: 2019-05-07
Posts: 3

Re: Shut down automatically after time machine backup

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

Offline

 

#5 2019-05-13 09:58:19 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 434

Re: Shut down automatically after time machine backup

GeorgeQ wrote:

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:

Applescript:

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:

Applescript:


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

Last edited by KniazidisR (2019-05-13 11:16:19 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#6 2019-05-17 09:32:36 am

GeorgeQ
Member
Registered: 2019-05-07
Posts: 3

Re: Shut down automatically after time machine backup

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:

Applescript:



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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)