Skip to main content

Systemd User Units as a Cronjob Alternative

Warning

Systemd user units are only available on our Managed Hosting plans. On Shared Hosting (Starter and Business plans), set up recurring tasks instead as cronjobs in LiveConfig.

Many applications, shop and CMS systems in particular, need tasks that run on a recurring schedule: processing queues, clearing caches, sending emails, updating exchange rates. Classically, a cronjob handles this. On our Managed Hosting systems, we recommend setting up your own systemd user units instead wherever possible, which you manage yourself, with no root privileges required.

Advantages over classic cronjobs

  • No overlapping runs: A cronjob starts strictly on schedule, regardless of whether a previous run is still active. If a task ever takes longer than expected, multiple runs can overlap and interfere with each other. systemd reliably prevents this: a new run only starts once the previous one has finished.
  • Centralized logging: Output automatically lands in the systemd journal and can be inspected with journalctl --user, including retroactively, without a separate log file or email sent by the cron daemon.
  • Automatic restart: If a service crashes, systemd can restart it on its own, depending on configuration.
  • Clear status: systemctl --user status shows at a glance whether a task last ran successfully.

Prerequisites

Carry out the following steps via SSH access on your hosting account. There is no separate root login, all commands run in your own user context via systemctl --user.

Details

For user units to keep running without an active SSH session, loginctl enable-linger is normally required. In our Managed Hosting environment, this so-called lingering is already enabled and managed by us, so no additional step is needed here.

First create the directory for your user units, if it doesn’t exist yet:

mkdir -p ~/.config/systemd/user

Two basic patterns

Depending on how the task behaves, one of two patterns fits best.

Pattern A: recurring single run (service + timer)

Equivalent to a classic cronjob: a script is invoked once on a fixed schedule, runs to completion, and exits. For this, you create two matching units.

~/.config/systemd/user/example.service:

[Unit]
Description=Example task

[Service]
Type=oneshot
ExecStart=/usr/bin/php8.2 /var/www/USERNAME/htdocs/example.com/cron.php

~/.config/systemd/user/example.timer:

[Unit]
Description=Example task (timer)

[Timer]
OnCalendar=*-*-* *:0/5:00
AccuracySec=1s
Persistent=false

[Install]
WantedBy=timers.target

OnCalendar replaces the five fields of a classic cron schedule:

Cron syntax OnCalendar equivalent Meaning
* * * * * OnCalendar=minutely every minute
*/5 * * * * OnCalendar=*-*-* *:0/5:00 every 5 minutes
*/15 * * * * OnCalendar=*-*-* *:0/15:00 every 15 minutes
*/30 * * * * OnCalendar=*-*-* *:0/30:00 every 30 minutes
0 * * * * OnCalendar=hourly hourly
30 2 * * * OnCalendar=*-*-* 02:30:00 daily at 02:30

Only the timer unit is activated, not the service unit directly, since activating the service directly would run it once immediately instead of waiting for its schedule:

systemctl --user daemon-reload
systemctl --user enable --now example.timer

Pattern B: a long-running service with its own internal scheduling

Some applications bring their own task management and should therefore not be restarted at short intervals, but instead be kept running continuously. The program itself exits after a defined run time, and systemd automatically restarts it afterwards. A single unit without a timer is enough here:

~/.config/systemd/user/example-loop.service:

[Unit]
Description=Example background service

[Service]
Type=simple
Restart=always
WorkingDirectory=/var/www/USERNAME/htdocs/example.com
ExecStart=-/usr/bin/php8.2 /var/www/USERNAME/htdocs/example.com/bin/console scheduled-task:run --time-limit=900 --memory-limit=512M
RestartSec=1s
StartLimitBurst=10000

[Install]
WantedBy=default.target

Here the service unit itself is activated, not a timer:

systemctl --user daemon-reload
systemctl --user enable --now example-loop.service

The leading hyphen before the program path in ExecStart tells systemd not to treat an exit code caused by the application itself as a start failure. StartLimitBurst=10000 prevents systemd from giving up after a few quick restarts in a row, for instance if the run time is occasionally very short.

Examples for common CMS and shop systems

The following examples show how the recurring tasks of well-known systems map onto one of the two patterns. Adjust USERNAME and the paths to match your own installation.

System Task Pattern ExecStart
Shopware scheduled-task:run B (long-running, Restart=always) /usr/bin/php8.2 .../bin/console scheduled-task:run --time-limit=900 --memory-limit=512M
Magento cron.sh every 5 minutes A (service + timer, OnCalendar=*-*-* *:0/5:00) /bin/bash .../cron.sh
WordPress wp cron event run --due-now every 30 minutes A (service + timer, OnCalendar=*-*-* *:0/30:00) cd .../wp-installation && wp cron event run --due-now
PrestaShop currency rate update, hourly A (service + timer, OnCalendar=hourly) /usr/bin/wget -O /dev/null -q "https://yourdomain.com/adminXXX/cron_currency_rates.php?secure_key=..."

A fully worked-out setup for several parallel tasks with individual schedules is also available in our article on cronjobs for your own Sendy installation.

Verify the setup

Overview of all active timers, including the next scheduled run:

systemctl --user list-timers

Status of a single unit:

systemctl --user status example.service

Follow a unit’s log in real time:

journalctl --user -u example.service -f

Further commands

systemctl --user start example.service
systemctl --user stop example.service
systemctl --user restart example.service
systemctl --user disable example.timer

If you need assistance, feel free to contact our Support.