32

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 foo@user.service look like in case I wanted to execute something every 30 minutes from boot/system start?

foo@user.service

[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... ???
38

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

12
  • 1
    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
    Apr 24 '15 at 19:30
  • 1
    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
    Apr 24 '15 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
    Jul 28 '16 at 2:58
  • 1
    Where is 30min set in your example? Aug 8 '17 at 14:45
  • 1
    @KarlMorrison it is set to 10s in example, change value to 30min for full satisfaction expirience
    – Reishin
    Aug 8 '17 at 15:29
17

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
  • 2
    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. Aug 5 '17 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
    Aug 5 '17 at 3:09
0

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>
1
  • Does this run "every 30 minutes from boot/system start"? How would the OP adapt it to do that?
    – Jeff Schaller
    Dec 1 '20 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.