Euan's Blog

Using Laravel's scheduled tasks with systemd timers

Laravel 5 includes a handy system to create scheduled tasks that are ran through the artisan console. In order to run these command automatically, the documentation recommends using a cron job to call the schedule:run artisan command every minute.

One problem: minimal CentOS 7 installs do not come with cron installed. We can easily replicate this functionality using systemd timers however, without having to install any extra dependencies. The best resource for learning more about systemd timers I've found is the Arch Linux Wiki Page "systemd/Timers", which goes into detail on the different types of timers (monotonic and real time) as well as providing some example unit files.

In our case, we want to use a monotonic timer that runs every 1 minute after boot (with a wait of 1 minute after boot) that will call the artisan command. In order to do so, we need to write a service unit file that will actually perform the command, /etc/systemd/system/laravel-cron.service:

[Unit]
Description=Run Laravel cron tasks
After=network.target

[Service]
User=http
ExecStart=/usr/bin/php /sites/www/artisan schedule:run

You'll need to change the User to the user that you use to run PHP or your webserver to ensure it has the correct file permissions to access any files your command may need to access.

You also need to add a timer unit file to call this service on a schedule, /etc/systemd/system/laravel-cron.timer:

[Unit]
Description=Run laravel timed tasks

[Timer]
OnBootSec=1min
OnUnitActiveSec=1m

[Install]
WantedBy=timers.target

Once you've created the unit files, reload systemd by calling systemctl daemon-reload, then enable your new timer:

systemctl enable laravel-cron

You can track your timer to see when it last fired and when it will next fire by running:

systemctl list-timers

You can also follow the logs for your timer via journald:

journalctl -f -u laravel-cron.timer

Or for the actual service:

journalctl -f -u laravel-cron.service

There are several other benefits to running your tasks via systemd timers such as being able to create service dependencies. I almost always prefer to use systemd over cron on any systems I set up.

#PHP