I recently moved to Arch (EndeavourOS) from Mint. Arch doesn’t have cron installed as it uses systemd timers instead, and while I could have just installed cron that felt like a lazy answer.

Systemd timers are easy enough to use and I got it working straight away, but I bumped into a comment in the Arch wiki about using a template for the timer so it can be re-used. I’m a bit slow, so I spent a hours trying to work this out, but I couldn’t find a good example. Anyway, I now have it working so I thought it would be useful for someone in the future for easy reference .

This is how you create a timer template that can be reused to run a oneshot service under a specific user. In this example it will run on the hour every hour.

Create the timer file.

sudo nano /etc/systemd/system/everyhour@.timer

Paste the following into that file, save and close.

[Unit]
Description=Run %i every hour

[Timer]
OnCalendar=*-*-* *:00:00
Persistent=true
Unit=%i.service

[Install]
WantedBy=timers.target

Create the service file for the script or command you want to run. (using “myscript” in this example)

sudo nano /etc/systemd/system/myscript.service

Paste the following into that file, save and close.

[Unit]
Description=My Script

[Service]
User=username
group=username
Type=oneshot
ExecStart=/usr/local/bin/command -parameters

Now enable and start the timer

sudo systemctl enable everyhour@myscript.timer
sudo systemctl start everyhour@myscript.timer
  • Oisteink@feddit.nl
    link
    fedilink
    arrow-up
    3
    ·
    9 months ago

    You could make a scripts that takes an argument, and that script executes all scripts in the folder provided as the argument. Then make 4 triggers that call this script with different folders as the argument. You could call these triggers something like hourly, daily, weekly and monthly.