Introduction
The Cron job scheduler is a command-line tool on UNIX and UNIX-like systems that allows users to schedule the automatic execution of shell scripts. Cron is often used for administration tasks, such as web server maintenance, running backups, and sending batch emails.
This article shows how to set up a Cron job on Mac.
Prerequisites
- A system running macOS.
- Access to the command line.
How to Set Up a Cron Job on macOS
Cron is a daemon that runs in the background and monitors changes made in the crontab file. When the user adds a Cron job to crontab, the daemon ensures that the job and the related script are executed at a provided time or interval.
The following sections show how to create a script, add a Cron job to the crontab file, and list the active Cron jobs.
Write Script
Cron executes commands provided through UNIX shell scripts. Follow the steps below to create a script:
1. Make a new .sh file using a text editor like Nano. The example below creates a file named test-script.sh:
nano test-script.sh
2. Format the script according to the shell it is using. For example, to create a Bash script, start with the shebang expression containing the path to the Bash binary. Then, list the commands that you want to execute:
#!/bin/bash
echo "This is an example Bash script."
Save the file and exit.
3. Make the script executable with the chmod command:
chmod +x test-script.sh
Create/Edit crontab File
Schedule the script in Cron by executing the following steps:
1. Open the crontab file:
crontab -e
The file opens in the vi editor. If the crontab had not previously existed, a new file is created.
Note: To schedule a job for a different user, add the -u option: crontab -u [username] -e
2. Add a line containing a Cron expression and the path to the script:
[cron-expression] [path-to-script]
A Cron expression consists of five fields that accept the respective number values:
[minute: 0-59] [hour: 0-23] [day: 1-31] [months: 1-12] [weekday: 0-6]
Note: The zero in the weekday field stands for Sunday, one for Monday, two for Tuesday, etc.
The syntax also allows the following special characters:
- (*) includes all values.
- (/) divides the value into steps, e.g., */2 in the day field means "every second day."
- (-) allows creating a range of values, e.g., 1-3 in the month field means "from January to March."
- (,) specifies individual values, e.g., 1,3 in the weekday field means "on Monday and Wednesday."
Note: Some Cron versions also accept timestamps, such as @hourly, @daily, and @monthly, as shorthand expressions that can replace standard Cron syntax.
For example, to create an expression that executes the test-script.sh located in your home directory every Tuesday and Thursday at 6 p.m., press i to turn on the Insert Mode and add the following line to the crontab file:
0 18 * * 2,4 /Users/[username]/test-script.sh
3. Save the file in vi and exit.
List Existing Cron Jobs
Once a job is created using the crontab file, the daemon reads and performs it according to the given schedule. Use the crontab command to list the currently active jobs on your system:
crontab -l
The output displays the previously added Cron expressions.
Cron Jobs on Mac: Examples
By combining numbers and special characters, Cron allows the creation of flexible expressions for job scheduling. The following section contains expressions that illustrate some of Cron's abilities.
Execute Script Every Twelve Hours
Add the following expression to the crontab file to schedule a job that will execute on the full hour every twelve hours:
0 */12 * * * [path-to-script]
Schedule Multiple Scripts to Execute Every Five Minutes
To run multiple scripts simultaneously, schedule them all in the same expression by separating them with semicolons. For example, to execute two scripts every five minutes, type the following expression:
*/5 * * * * [path-to-script1]; [path-to-script2]
Perform Task on Saturday and Sunday
Separate values with a comma to add more than one value in the same field of the Cron expression. For example, enter the expression below to run a job at noon every Saturday and Sunday:
0 12 * * 6,0 [path-to-script]
Execute Script Every Hour During Work Hours
You can combine more than one interval within the same expression. The following statement schedules the script to run every hour from 9:00 a.m. to 5:00 p.m. on workdays:
0 9-17 * * 1-5 [path-to-script]
Scheduling Jobs Spanning Two Days
To execute a job on a schedule that spans two days, use two Cron expressions for the same script. For example, to run a script every hour from 5:30 p.m. on Wednesday until 5:30 a.m. on Thursday, type the following:
30 17-23 * * 4 [path-to-script]
30 0-5 * * 5 [path-to-script]
Conclusion
This tutorial presented the Cron daemon, a tool for automating script execution on macOS. The article explained the Cron syntax and provided examples illustrating the scheduler's capabilities.
If you use Kubernetes, read our Kubernetes CronJob Guide and learn to use Cron syntax to perform cluster maintenance.