2

I have a script that updates my google drive. I made a systemd unit to run this script, and a timer that runs the unit every 10 seconds, which both work. However, when I get disconnected from internet, the script fails and systemd stops running the it even if the internet comes back on. Is there any way I can make systemd keep on running the script, or is there a way to have systemd run the script only if there is an internet connection?

Here are the files

/etc/systemd/system/grive.service:

[Unit]
Description=Syncronize google drive folder

[Service]
User=my_name
ExecStart=/home/my_name/bin/update-grive

/etc/systemd/system/grive.timer:

[Unit]
Description=Timer for how often to syncronize google drive folder

[Timer]
OnUnitActiveSec=10s
OnBootSec=10s

[Install]
WantedBy=timers.target

/home/my_name/bin/update-grive:

#!/usr/bin/env bash
cd /home/my_name/gdrive
grive
  • How do you expect to know when you are connected to the internet? – Stephen Rauch Mar 30 '17 at 4:13
  • 1
    if ping -c 3 google.com ; then ./script.sh ; fi – Archemar Mar 30 '17 at 5:02
  • Consider some inotify(7) based approach – Basile Starynkevitch Mar 3 '18 at 13:33
2

Add Restart=always to the service unit, so systemd will keep bringing up the service if it crashes.

On a side note you should use OnUnitInactiveSec instead of OnUnitActiveSec.

OnUnitInactiveSec=10s (or 20s) will start the service 10 seconds after it stopped. This way you make sure it doesn't get called twice and possibly avoid banning for DOSing google

  • Wouldn't the script still fail if ping didn't work. It would say unknown service google.com and return an error code right? – Vityou Mar 30 '17 at 17:08
  • Nope. Run this oneliner in you shell to see it for yourself: ping -c3 -q asdfgasdfg.com; if [ $? != 0 ]; then echo "failed but still here"; fi. If the script failed as you suggest, the echo statement would not run – Bruno9779 Mar 30 '17 at 17:33
  • I tried something like that using racket, it printed the stuff after the failed ping, but systemd still counted it as failed. It must be different in bash or something, I'll try it on my linux machine as soon as I can. – Vityou Mar 30 '17 at 19:03
  • You need space around the brackets, and it still fails when there is no internet. – Vityou Mar 30 '17 at 23:42
  • However, your Restart=always works. I can accept the question if you delete the part about adding ping, or replace it with something that works. – Vityou Mar 30 '17 at 23:55
0

Under [Service], add:

Restart=always
RestartSec=10
0

Make sure to set the accuracy to seconds, like this:

AccuracySec=1min

As per the docs:

AccuracySec= Specify the accuracy the timer shall elapse with. Defaults to 1min. Source: https://www.freedesktop.org/software/systemd/man/systemd.timer.html#Description

Do this in your .timer file.

I've struggled with this for quite a while.

  • Don't you mean AccuracySec=1s? – schumacher574 Mar 13 at 19:35

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.