1

I recently lost configuration of one of my IDEs (deleted it myself, I am very smart). I had a backup but it was a few months old. I have a script to backup all my config and push it to GitHub. I was looking into a way to run a script every week to back up everything automatically but launchd / cron need a specific time to run. My laptop might not be running at that time but I do boot up my laptop every weekend at least once.

To make it clear, my problem differs substantially from this question talks about running a script "10 minutes after 2 am every weekday only". I’m not sure how to adapt it to my need to run only once per weekend.

Is there a way to configure launchd / cron to run on a weekend at any time to back up my configuration?

bmike
  • 235,889
Sourabh
  • 151
  • 1
  • Not a duplicate. I don't want a script to run at a specific time, just anytime on a weekend. – Sourabh Jul 21 '18 at 15:13
  • 1
    There's no random time that can be given as an interval. However, if it doesn't run at a particular schedule, it will do so the next time it's run. – Allan Jul 21 '18 at 15:52
  • "if it doesn't run at a particular schedule, it will do so the next time it's run." didn't get that. You mean if a cron of 4:20 Sunday didn't run because laptop was off then it'll run whenever laptop is turned on, on a sunday? – Sourabh Jul 21 '18 at 19:49
  • Yes. It will run the very next time. – Allan Jul 21 '18 at 19:54
  • Source? According to this question it won't. – Sourabh Jul 21 '18 at 22:36
  • Not off, but asleep. See: Apple Developer and this AD post. You can get around this by including the directive RunAtLoad so that it runs every time you power up the machine and on the scheduled interval. However, what difference does it make since you want it to happen randomly in the first place? It's far more powerful than cron. – Allan Jul 21 '18 at 23:07
  • In the past, adding a StartCalendarInterval was all that was needed to have tasks missed while sleeping execute, so you would make a tack for the first minute you would want the backup to start and be on the hook for waking it before the power went off. It should execute that weekend if you woke it, and later in the week if the Mac sleeps all weekend. https://apple.stackexchange.com/q/37905/5472 – bmike Feb 04 '24 at 16:13

2 Answers2

1

Set your computer to power on or wake from sleep on a specific day and time (Energy Saver preferences), then run your script though launchd or cron within that time frame.

fd0
  • 10,708
  • It's a laptop (MacBook). Will the EnergySaver work even if the lid is down? – Sourabh Jul 21 '18 at 15:07
  • A MacBook will sleep when the lid is closed – unless mains power and an external monitor are connected. Otherwise, no third party software will run while the lid closed. – Graham Miln Feb 05 '24 at 12:53
1

Don't rely on the OS's task scheduler to handle Sleep, Power Off or other states that your computer may be in when the time comes to run the script. Build it into the script instead - and run it often (perhaps hourly). That way you are pretty much sure that is is run at some point the computer is active.

"But I don't need hourly backups!"

No, so let the script begin by checking if it has run already this [hour,day,week,month]. If it has, let the script terminate. The check could be just checking the timestamp of the last backup files.

  • 1
    This is my feeling - put the smarts in the script and have launchd kick it off every hour efery day it’s awake and then review the logs after a bit. To be honest, why not use Time Machine since after a month you have your weekly backups nicely thinned and maintained. – bmike Feb 04 '24 at 15:50