47

I would like to execute a script every 30 min after booting into the system. I know you can use cron, but I don't plan to use this feature often therefore I'd like to try it with systemd.

So far I have only found the monotonic timers which allows to execute something once (at least I think so). How would the foo.timer and [email protected] look like in case I wanted to execute something every 30 minutes from boot/system start?

[email protected]

[Unit]
Description=run foo
Wants=foo.timer

[Service]
User=%I
Type=simple
ExecStart=/bin/bash /home/user/script.sh

foo.timer

[Unit]
Description=run foo

[Timer]
where I am stuck... ???

3 Answers 3

59

You need to create two files: one for service, other for timer with same name.

example:

/etc/systemd/system/test.service

[Unit]
Description=test job

[Service]
Type=oneshot
ExecStart=/bin/bash /tmp/1.sh

/etc/systemd/system/test.timer

[Unit]
Description=test

[Timer]
OnUnitActiveSec=10s
OnBootSec=10s

[Install]
WantedBy=timers.target

after that reload the systemd using command systemctl daemon-reload and start your timer by systemctl start test.timer, or enable it by default (systemctl enable test.timer).

test content of 1.sh

#!/bin/bash
echo `date` >> /tmp/2

And command to check all available timers: systemctl list-timers --all

More detailed info on project page and examples on ArchLinux page

15
  • 2
    please use systemctl list-timers --all command and check the output. He need to be like this. Look to unit, left and passed columns. If timer is present, please look to your service file and check for bugs there since timer working normally.
    – Reishin
    Commented Apr 24, 2015 at 19:30
  • 2
    no, coz main question doesn't ask nothing about "notify-send" and i think we should not mix two different things when such topic already present. in your case, try to add export DISPLAY=:0.0 to the script.
    – Reishin
    Commented Apr 24, 2015 at 20:26
  • 1
    ps: according to man systemd.timer the Persistent=true only has an effect on configured with OnCalendar (i.e. wallclock)
    – snyh
    Commented Jul 28, 2016 at 2:58
  • 3
    Where is 30min set in your example? Commented Aug 8, 2017 at 14:45
  • 3
    @KarlMorrison it is set to 10s in example, change value to 30min for full satisfaction expirience
    – Reishin
    Commented Aug 8, 2017 at 15:29
25

Here is another option without using your timer. If timing is not terribly critical and and script isn't long running it will be fine for simple things.

[Unit]
Description=Run foo

[Service]
User=%I
Restart=always
RestartSec=1800s
ExecStart=/bin/bash /home/user/script.sh
2
  • 6
    I like this solution. The only major drawback is that system logs will be flooded with "Starting <service X>" logs if you restart often (i.e. every 30 seconds). At which point it might be better to run the service as a daemon instead, instead of restarting the service over and over from systemd. Commented Aug 5, 2017 at 0:36
  • That's true. For quick and simple stuff it works. But the timer or a long running script would be a better solution.
    – hookenz
    Commented Aug 5, 2017 at 3:09
10

The proper way is to use systemd-run you can schedule your job without the need to define your own unit.

It allows you to schedule by a calendar or every period of time. Assuming MYSELF is your full path application:

systemd-run --user --on-calendar '*:0/1' ${MYSELF} <args>
3
  • Does this run "every 30 minutes from boot/system start"? How would the OP adapt it to do that?
    – Jeff Schaller
    Commented Dec 1, 2020 at 2:38
  • 1
    @JeffSchaller I'm about to try this out, but it looks like OP's requirement could be met with systemd-run --on-boot=1800 --on-unit-active=1800 /home/user/script.sh
    – Tony Park
    Commented Nov 18, 2021 at 14:40
  • 1
    This should be the accepted answer, as op stated he wants to run a script, not explicitly create a service and a timer. systemd-run does that in the background without the user noticing.
    – m.w.
    Commented Sep 15, 2022 at 9:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .