18

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... ???
22

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 them by default.

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

  • systemd accepts the scripts and it is listed, however nothing is happening – TomTom Apr 24 '15 at 19:12
  • which one? there should be two scripts, one timer and another service. Time, when they was executed could be checked by list-timers command, Possible errors could be checked by systemctl status test.timer and systemctl status test.service comamand – Reishin Apr 24 '15 at 19:19
  • 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
10

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
    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. – jersey bean 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. – Matt H Aug 5 '17 at 3:09

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.