keepcalculator

06 Numbers and Powers

Cron Job on Mac: Setup, Examples & Fixes

Card No. 06·73343 · Updated · Standard formula, shown below

Cron jobs on macOS let you schedule commands or scripts to run automatically at specific times, using the built-in crontab utility. You set them up by editing your user’s crontab file with crontab -e, and the system’s cron daemon (launchd in modern macOS) runs them in the background. This guide covers the syntax, common examples, troubleshooting, and the key differences from launchd.

What Is Cron and Does macOS Still Use It?

Cron is a Unix-based job scheduler that has existed since the 1970s. On macOS, the cron daemon is still present and functional, but Apple has been steering users toward launchd, the native service management framework. Since macOS 10.4 (Tiger), launchd has been the recommended way to schedule tasks, but cron remains fully supported for backward compatibility. You can still use crontab commands and the cron daemon will execute them, though Apple’s documentation suggests launchd for new projects.

The main practical difference: cron jobs run at the system’s scheduled times, but if your Mac is asleep or powered off, the job is missed. Launchd can be configured to run a job when the Mac wakes up, which is often more reliable for laptops. For simple, time-based scheduling, cron works fine.

How to Create a Cron Job on Mac

Creating a cron job involves editing your user’s crontab file. Here’s the step-by-step process:

  1. Open Terminal (found in /Applications/Utilities/).
  2. Type crontab -e and press Enter. This opens your crontab in the default text editor (usually vim or nano, depending on your system).
  3. If you’re new to vim, press i to enter insert mode. For nano, just start typing.
  4. Add a line with the schedule and command (see syntax below).
  5. Save and exit: in vim, press Esc, then type :wq and Enter. In nano, press Ctrl+O, Enter, then Ctrl+X.
  6. To see your current crontab, type crontab -l. To remove it, type crontab -r.

Each line in the crontab has five time fields followed by the command to run. The fields are: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-7, where 0 and 7 are Sunday).

Cron Syntax and Examples

The basic syntax is:

minute hour day-of-month month day-of-week command

Here are common examples:

  • 0 9 * * * /usr/bin/backup.sh – Run at 9:00 AM every day.
  • */15 * * * * /usr/bin/check_status.sh – Run every 15 minutes.
  • 0 2 * * 1 /usr/bin/cleanup.sh – Run at 2:00 AM every Monday.
  • 30 18 * * 5 /usr/bin/report.sh – Run at 6:30 PM every Friday.
  • 0 0 1 * * /usr/bin/monthly.sh – Run at midnight on the first day of every month.

Use asterisks as wildcards. A comma separates multiple values (e.g., 1,15,30), a hyphen defines a range (e.g., 9-17), and a slash means “every” (e.g., */10 for every 10 minutes).

Common Problems and How to Fix Them

Cron jobs on Mac often fail silently. Here are the most frequent issues:

  • Job doesn’t run at all: Check if the cron daemon is running by typing pgrep cron in Terminal. If it returns nothing, start it with sudo launchctl load -w /System/Library/LaunchDaemons/com.vix.cron.plist (older macOS) or sudo launchctl enable system/com.vix.cron (newer).
  • Command not found: Cron uses a minimal PATH. Always use the full path to your command or script. For example, use /usr/local/bin/python3 instead of python3. To find the full path, type which python3 in Terminal.
  • Script runs but does nothing: The script may lack execute permissions. Run chmod +x /path/to/script.sh to make it executable.
  • Output goes nowhere: By default, cron sends output to your local email (which may not be configured). Redirect output to a log file: 0 9 * * * /path/to/script.sh >> /tmp/cron.log 2>&1.
  • Job runs at wrong time: Check your system time zone with date. Cron uses the system’s local time, not UTC, unless you specify it in the command.

Cron vs. Launchd: Which Should You Use?

While cron works, launchd is Apple’s recommended approach. Here’s a comparison:

  • Reliability: Launchd can catch up on missed jobs (e.g., if the Mac was asleep). Cron simply skips them.
  • Configuration: Cron uses a simple text file; launchd uses XML property lists (.plist) stored in ~/Library/LaunchAgents (for user jobs) or /Library/LaunchDaemons (for system jobs).
  • Features: Launchd supports dependencies, environment variables, and can run jobs at login or when a file changes. Cron is purely time-based.
  • Learning curve: Cron is easier for beginners; launchd has more options but is more verbose.

For a one-off simple schedule, cron is fine. For anything that must run reliably on a laptop, launchd is better.

How to Test and Debug a Cron Job

Before relying on a cron job, test it manually. Run the exact command in Terminal to see if it works. Then add a test line to your crontab that runs every minute: * * * * * echo "test" >> /tmp/cron_test.log. Wait a minute and check the log file. If it appears, cron is working. If not, check the cron daemon and your PATH.

For scripts, add set -x at the top to trace execution, and redirect all output to a log file. You can also check the system log for cron errors: grep cron /var/log/system.log (on newer macOS, use log show --predicate 'process == "cron"').

Security and Permissions

Cron jobs run with your user’s privileges, so they can access your files. Be careful with commands that delete or modify data. If you need to run a job with root privileges, you can edit the root crontab with sudo crontab -e, but this is risky. Always test thoroughly.

Also, macOS may block cron jobs if the Terminal app doesn’t have “Full Disk Access” in System Settings > Privacy & Security. This is a common issue on recent macOS versions, especially for jobs that access protected folders like Desktop, Documents, or Downloads. To fix, add your Terminal (or the app running the script) to the Full Disk Access list.

Frequently Asked Questions

How do I edit my crontab on a Mac?

Open Terminal and type crontab -e. This opens your personal crontab file in the default editor (usually vim). Press i to insert, add your lines, then press Esc and type :wq to save and exit. To view your crontab, use crontab -l.

Why won’t my cron job run on macOS?

Common causes: the cron daemon isn’t running (check with pgrep cron), the command path is wrong (use full paths), the script isn’t executable (chmod +x), or macOS permissions block it (grant Full Disk Access to Terminal). Redirect output to a log file to see error messages.

Can I use cron to run a job every minute?

Yes, use * * * * * command. This runs the command at the start of every minute. Be careful not to overload your system with frequent jobs.

What’s the difference between cron and launchd on Mac?

Cron is a simple time-based scheduler that runs jobs at specified times, but misses them if the Mac is asleep. Launchd is Apple’s native system that can run jobs at login, when a file changes, or catch up on missed runs. For most users, cron is easier, but launchd is more powerful and reliable.

How do I stop a cron job?

Edit your crontab with crontab -e, delete the line for that job, and save. To remove all cron jobs, use crontab -r. This deletes your entire crontab, so use it carefully.

Does cron work when my Mac is asleep?

No. If your Mac is asleep or powered off at the scheduled time, the job is skipped. Launchd can be configured to run the job when the Mac wakes up, which is why it’s recommended for laptops.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →