Sometimes I just want to make a quick cronjob. And with SysV, this was a cinch! But now with systemd, I have to make a timer file, a service file, and run a few systemctl commands.
Service and Timer file
To do so you have to create a service and a timer file.
The service file will contain the command and any needed things, like the user to run it under. Give it whatever name you want to, and you can use these templates:
name-of-your-cron-script.service
[Unit]
Description=The Description of what your Cronjob does
Conflicts=shutdown.target
After=local-fs.target time-sync.target
Before=shutdown.target
# run as this user
# User=username
[Service]
Type=oneshot
ExecStart=/path/to/executable -o "your option here" --any-thing-else
name-of-your-cron-script.timer
[Unit]
Description=The Description of what your Cronjob does
[Timer]
# on boot
OnBootSec=15min
# How often to run it
OnUnitActiveSec=1h
#OnUnitActiveSec=1d
#OnUnitActiveSec=1w
# If the machine is not on during the set time, run asap
# Persistent=true
# Set a random delay before running
# so multiple cronjobs do not run at once.
RandomizedDelaySec=1800
[Install]
WantedBy=timers.target
You install these (as a custom cronjob) in `/etc/systemctl/system
Then reload systemd and load the cronjob:
systemctl daemon-reload
systemctl enable name-of-your-cron-script.timer
systemctl start name-of-your-cron-script.timer
To list the current active and inactive cronjobs and their timers:
systemctl list-timers --all
Using crontab with Systemd-cron-next
To get the old functionality of the crontab, and the cron.hourly, cron.daily, and other cron folders, you can use Systemd-cron and Systemd-cron-next. You only need one or the other.
All the package does is take your crontab and the hourly, daily, weekly, yearly, onboot files, and it will create your .timer and .service files for you. And it supports emailing errors so you can get notified if a cron fails.