29
submitted 9 months ago* (last edited 9 months ago) by [email protected] to c/[email protected]

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/[email protected]

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 [email protected]
sudo systemctl start [email protected]
top 1 comments
sorted by: hot top controversial new old
[-] [email protected] 3 points 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.

this post was submitted on 01 Oct 2023
29 points (96.8% liked)

Arch Linux

7173 readers
12 users here now

The beloved lightweight distro

founded 4 years ago
MODERATORS