How to schedule recurring tasks on macOS via cron

Last modified: March 28, 2025

You can automate recurring tasks on your Mac using the cron command-line utility, which allows scheduling the execution of jobs (commands or shell scripts) at specific times. The actions of cron are driven by the crontab (cron table) configuration files that store your scheduled jobs (also known as cron jobs). Whenever you add a cron job to a crontab file, the cron daemon ensures its periodical execution.

You can use cron alongside dbForge Studio, installed on macOS via CrossOver, to automate database backups, data and schema comparisons, and other database-related tasks.

Get started

First, open the Terminal and run the following command to check the list of your current scheduled jobs.

crontab -l

In our case, we can see that there are no scheduled jobs yet.

Check the list of cron jobs

In order to set up the recurring execution of a job, you can either add the corresponding script directly to crontab or create a dedicated file that will contain the said script and simply provide crontab with the link to this file.

The latter option is more convenient, so we will go with it.

Create a script

To create a script for recurring execution, take the following steps.

1. Open Terminal and create a new .sh file using any text editor you like, e.g., nano.

nano demo-script.sh

Create a script

2. Enter the required instructions into the file and format your script according to the shell you are using. For instance, if you want to create a Bash script, start with the shebang expression, which is a combination of bash # and bang ! followed by the Bash shell path.

#!/bin/bash
echo "This is an example Bash script."

Bash script

Note

Here are more specific script examples for several most common database-related tasks.

3. Finally, you need to make this script executable by adding permissions via the chmod command.

chmod +x demo-script.sh

The chmod command

Create a cron job

Now that you have created a file and the script for recurring execution, you need to add it as a cron job.

1. Open the crontab file in Terminal.

crontab -e

After you run this command, the crontab file will be opened in the vi editor. In case there is no crontab file, it will be created automatically.

2. Add a line that contains the required cron expression and the path to your script file.

A cron expression is a string of five fields separated by spaces. It helps you specify the time for the recurring execution of your script.

* * * * *

Each field represents a unit of time.

[minute: 0-59] [hour: 0-23] [day: 1-31] [month: 1-12] [weekday: 0-7; both 0 and 7 represent Sunday, 1 - Monday, and so on]

To create more complex schedules, you can use the following characters.

* is a wildcard that includes all values; for instance, you can enter * in the hour field to run the job every hour.

/ specifies intervals; for instance, you can enter */2 in the day field to run the job every second day, or enter */30 to run the job every 30 minutes.

- specifies a range of values; for instance, you can enter 1-3 in the month field to run the job in January, February, and March.

, specifies comma-separated values; for instance, you can enter 1,3 in the weekday field to run the job on Monday and Wednesday.

You can combine all of the above to specify the required time most precisely. Here is an expression that will run our demo-script.sh file every Monday, Wednesday, and Friday at 21:00.

0 21 * * 1,3,5 /Users/jordansanders/demo-script.sh

Enter the cron expressions

3. Save the crontab file with :wq.

4. To double-check the list of cron jobs, you can run crontab -l once again.

Double-check the list of cron jobs

That’s it. Below you will find a few examples of scripts for database tasks that can be scheduled via cron.

Script example: Scheduled backup

The following example runs a database backup from the command line. You can schedule it for recurring execution using the workflow described above. You only need to adjust the CrossOver bottle name, the path to the Studio, your connection credentials, and the output path.

/Applications/CrossOver.app/Contents/SharedSupport/CrossOver/bin/wine --bottle 'dbForge Studio for MySQL' --check --wait-children --start "C:\Program Files\Devart\dbForge Studio for MySQL\dbforgemysql.com" /backup /connection:"User Id=[User name];Host=[host];Character Set=utf8" /database:sakila /password:[Password] /outputfile:"C:\users\crossover\Documents\Backup\[file_name].sql"

Note

For a detailed breakdown of this script, refer to Command-line mode on macOS.

Script example: Scheduled data comparison

Similarly, the following example runs a data comparison from the command line. You can also adjust and schedule it for recurring execution using the workflow described above.

/Applications/CrossOver.app/Contents/SharedSupport/CrossOver/bin/wine --bottle 'dbForge Studio for MySQL' --check --wait-children --start "C:\Program Files\Devart\dbForge Studio for MySQL\dbforgemysql.com" /datacompare /source connection:"User Id=[User name];Host=[Host];Password=[Password];Database=sakila;Character Set=utf8" /target connection:"User Id=[User name];Host=[Host];Password=[Password];Database=sakila;Character Set=utf8" /sync

Script example: Scheduled schema comparison

This example runs a schema comparison from the command line. You can also adjust and schedule it for recurring execution using the workflow described above.

/Applications/CrossOver.app/Contents/SharedSupport/CrossOver/bin/wine --bottle 'dbForge_MySQL' --check --wait-children --start "C:\Program Files\Devart\dbForge Studio for MySQL\dbforgemysql.com" /schemacompare /source connection:"User Id=[User name];Host=[Host];Password=[Password];Database=sakila;Character Set=utf8" /target connection:"User Id=[User name];Host=[Host];Password=[Password];Database=sakila;Character Set=utf8" /sync