Search code examples
macosunixcroncron-task

In which of these scenarios will my crontabs run, when won't they?


Beginner here. I have a few questions regarding crontabs, on mac / osx (if it makes a difference). I'd like to understand in what scenarios my crontab will run, and when it doesn't.

I do know that this is true:

  • When I turn off my computer, the cronjob won't be executed.

That was the easy part. But what about these cases:

  1. The terminal is not open, but I am logged in
  2. The machine is running, but I am not logged in
  3. The cronjob is set for 9am, but my computer is off at the time, and I turn it on & log in at 10am

When will it work, when won't it?


Solution

  • cron jobs run while the computer is on & not sleeping, so it will run in situations 1 and 2. If the computer is off or asleep at the job's scheduled time, it does not do any sort of catch-up run later when the computer restarts/wakes up; therefore, it will not run in situation 3.

    Let me also clarify about situations 1 and 2: cron jobs run independently of any user login sessions, and any programs they're running. They can't read from Terminal input, anything they print won't show up on screen, and since they aren't part of your login session they have a limited ability to interact with the regular graphical user interface and running programs. They live in a semi-separate world from the programs (including Terminal commands) you run interactively.

    Note that crontabs are generally deprecated on macOS; the preferred way to run programs automatically is with launchd. But the launchd equivalent of a user crontab, called a Launch Agent, does run as part of a logged-in user session (and -- mostly -- gets skipped when the user is not logged in). And also, launchd jobs do get run if a scheduled run gets missed because the computer is off/sleeping. So they're quite a bit different from cron jobs.