Friday, November 22, 2024

applescript – Execute missed launchd job upon next startup

I would like to make periodic backups using rsnapshot on my MacBook. However, as it is a laptop, it will only be powered on at random times. Thus, scheduled tasks might be missed while it is powered off. A solution for this would be anacron which does not seem to be available on macOS though. Apparently macOS’ native launchd service is not capable of scheduling jobs in the manner of anacron. Here it says:

If you schedule a launchd job by setting the StartCalendarInterval key
and the computer is asleep when the job should have run, your job will
run when the computer wakes up. However, if the machine is off when
the job should have run, the job does not execute until the next
designated time occurs. All other launchd jobs are skipped when the
computer is turned off or asleep; they will not run until the next
designated time occurs.

If my monthly rsnapshot backup is, for example, scheduled for the first day of every month but the laptop is powered off that day, that would mean I lack that backup and only get the next one a whole month later.

Is there any workaround to this? Perhaps linking the launchd schedule with some conditional commands in a bash script?

The only idea I had to solve this was to schedule the launchd job for every single day at a specific time and execute a script which:

  1. Checks a timestamp log file if a successful backup has run this month
  2. If yes –> exit, if no –> run backup
  3. If backup successful –> add timestamp to that file

Though that seems unnecessarily complex in comparison to anacron jobs that you can create on Unix systems.

Related Articles

Latest Articles