Systemd Timer Units as alternative to cronjob?

Hi,

I’m having some trouble to make my cronjob run as you can see here.

Somebody suggested to use systemd timer units instead.
Is that a possibility or is it by any circumstances mandatory to run cron with one of the three given options?

Thanks for the replies!

Best regards

I managed to make the cronjob work, but I still would like to know if it also can be done with systemd and why there are only those three options (AJAX, Web, cron)?

Best regards

Hey proton,

works fine here.

First create a systemd service file: nextcloud-cron.service

[Unit]
# Execute nextcloud cron every 15 minutes.
Description=nextcloud-cron

[Service]
User=http
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/bin/bash -c php -f /srv/http/nextcloud/cron.php

Next create timer for this service: nextcloud-cron.timer

[Unit]
# Calls Nextcloud cron every 15 Minutes.
Description=nextcloud-cron

[Timer]
OnCalendar=*:0/15:00
Persistent=true
Unit=nextcloud-cron.service

[Install]
WantedBy=basic.target

Don’t forget to adjust your nextcloud User= and path in the ExecStart Statement of the service file.

Copy both files in the /etc/systemd/system directory and activate the timer. - Done.

Happy new year angerm! :slight_smile:

Thank you very much for this detailed explanation! :smiley:

Best regards

Hi,

Could you please explain the advantage of systemd over cron? Is it suggested to rather use systemd?

Thanks

Hello,

in which case I think you can use the service of your choice. They both do a good job here.

However, if the service is to be monitored in the sense of a daemon, systemd has advantages here:

systemd starts the service and also monitors whether it is still running. If desired, systemd can also restart the service automatically after a crash.
Dependencies at system startup are detected and, if necessary, the start of a service is written to a pipe until the calling service is ready. This speeds up the start enormously, because almost everything can be started in parallel. Everything is logged in the log.

With a little practice, service and timer units are quickly created and very flexible.

In almost all distributions, systemd is now standard. Therefore, a systemd service can be run on many installations without additional dependencies.

It is just a new and almost everywhere available method to start the system and services on Linux machines.

greetings …

2 Likes

Fails because bash -c only takes one argument. Only the php is run, the rest is ignored.

Demonstration (try it yourself and see):

This does not put foo in in the journal, see journalctl -b- 0
That is because bash -c echo foo only takes one argument, the foo is discarded.

$ cat whitespace.service 
[Unit]
# Execute nextcloud cron every 15 minutes.
Description=Use a systemd timer in lieu of cron.

[Service]
User=http
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/bin/bash -c 'echo foo'

This puts foo in the journal, see journalctl -b- 0.
That is because using single-quotes around ‘echo foo’ causes it to be one argument.

$ cat whitespace.service 
[Unit]
# Execute nextcloud cron every 15 minutes.
Description=Use a systemd timer in lieu of cron.

[Service]
User=http
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/bin/bash -c 'echo foo'