In this short guide you will learn how to use the sail cron
command to list,
create and delete system cron/crontab entries on your Sail server.
To get a list of active cron jobs in a Sail project, run:
sail cron list
Sample output:
# System Cron Entries
- id: b1c98922-d873-4ee4-8f84-93b8773367fa
user: www-data
schedule: */5 * * * *
command: wp cron event run --due-now
This command will show the existing cron tasks. Each entry will have a uinque
id which you can use to delete. You can also add the --json
flag to get
the list of cron entries in JSON format.
Note, that list will contain only cron entries added and managed by Sail CLI. If
you manually add a system cron entry using crontab
or /etc/cron.d
, it will
not show up in this list.
To add a new cron entry, use the following syntax:
sail cron add <recurrence> <command>
The recurrence
argument is a cron schedule expression, you can learn more
about these on crontab.guru. There are a few shortcuts
for convenience as well:
hourly
twicedaily
daily
weekly
The command
argument is the command that you wish to run. You'll need proper
escaping if using quotes and other special characters in your command, as well
as the recurrence.
For example, to run the wp my-command
WP-CLI command every 10 minutes, you can
add it to the system cron like so:
sail cron add */10 wp my-command
To run a command every Sunday, use:
sail cron add "0 0 * * 0" "wp my-command 'with arguments'"
Add the --root
flag if you need to run the command as root
, otherwise it
will run as the www-data
user by default, which is the recommended method.
To delete a Cron entry managed by Sail, run:
sail cron delete <id>
Where id
is the unique cron entry identifier from sail cron list
.
You can test a cron entry with:
sail cron run <id>
Where id
is the unique cron entry identifier from sail cron list
.
When provisioning a new project with Sail, a default cron entry is automatically added to the list to spawn the WordPress Cron using WP-CLI. This event runs once every five minutes, which is more than enough for most WordPress use cases.
However, if you wish to run the WordPress cron more often, feel free to remove
the entry (you can get the id from sail cron list
) and add a new one with a
higher frequency:
sail cron delete <id>
sail cron add */1 wp cron event run --due-now
This will spawn the WordPress cron every minute.
If you need assistance with your cron jobs in Sail CLI, please refer to our getting help section.