Magento Cron via systemd timers
While preparing a new server for a Magento merchant running Debian 12, I realized that the cron package is no longer shipped with Debian. It is still installable, but the default way of running cron jobs seems to be via systemd.
In systemd speak, cronjobs are called timers. A timer schedules the run of a systemd service, which means we need to configure a systemd service first.
In /etc/systemd/system
, create a new file (service unit) called magento.service
with the following contents:
# This service unit is for running Magento Cron jobs
[Unit]
Description=Runs the Magento cron jobs
Wants=magento.timer
[Service]
Type=oneshot
ExecStart=/usr/bin/sh /var/www/html/magento/current/bin/cron.sh
User=www-data
Group=www-data
[Install]
WantedBy=multi-user.target
The service section defines the script that gets executed. This script contains the call to Magento's cron command:
#!/bin/bash
/usr/bin/php8.2 /var/www/html/magento/current/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /var/www/html/magento/current/var/log/magento.cron.log
To instruct systemd to run the magento
service unit in regular intervals, we need to create an additional file called magento.timer
in the same /etc/systemd/system
directory:
# This timer unit is for the Magento cron service
[Unit]
Description=Executes every minute
Requires=magento.service
[Timer]
Unit=magento.service
OnCalendar=minutely
[Install]
WantedBy=multi-user.target
The timer interval is specified via the OnCalendar
property. You can either use shortcuts like minutely
, or hourly
. Or, as an alternative, you define the interval in a structure like this *-*-* *:*:00
To start the service unit and enable the timer, the following commands need to be executed as root user:
systemctl start magento.service
systemctl enable magento.timer
Even though the service unit you created is usually triggered by the timer, you can also trigger it manually by running systemctl start magento.service
at any time.