osx - launchagent vs cronjob vs calendar alarm

So I was wondering why would you choose one over the other on a mac if they essentially do the same thing? Which is run a script on a specific time or frequency. Does one of them actually wake up the sleeping mac and execute?

why would you use one over the other?

  • launchagent
  • cronjob
  • calendar alarm

thanks


Solution 1:

Prefer launchd

For non-graphical scripts, launchd is Apple's preferred approach.

cron is available but no longer recommended by Apple. This is for energy efficiency reasons as cron checks every minute for due tasks.

In comparison to cron, launchd's time based trigger is limited but this allows launchd to reliably calculate an absolute date and time of future events. Knowing these dates, launchd can remain idle until they arrive.

Calendar.app's alarm is useful for graphical scripts, such as AppleScript and Automator. For this to work, your Mac must be logged in and awake.

Graphical versus non-graphical scripts

Graphical scripts are those that interact with running Mac applications. These applications are assumed to have windows and a user interface. This means they require a graphical user session to work – and thus a user must be logged in for the application to be launched and kept running.

Graphical scripts include AppleScript and Automator.

Non-graphical scripts are those that limit their actions to command line tools and processes. They do not require a user to be logged in. The computer only needs to be awake for the script to complete its task.

Non-graphical scripts include shell scripts and command line tools.

Waking or powering on for a script

None of the built-in tools will power on or wake your Mac to perform your script. You will need a third party tool, such as Power Manager for this. I am affiliated with Power Manager, so feel free to get in touch with any questions.