3

I have a systemd service called conWlan0.service which establishes WiFi connection. Now I want to create another Unit which periodically checks the connection. Is this a reasonable start?

[Unit]
Description=...
After=conWlan0.service
OnFailure=conWlan0.service

[Service]
Type=simple
ExecStart=/usr/bin/checkWiFi
RestartSec=120
Restart=always

The checkWifi script exits 0 if everything is correct and an error code otherwise. Any suggestions?

1 Answer 1

1

If you want something to run periodically, you should use a SystemD timer unit. Having a service unit that keeps restarting is not a good idea because basically you have your system always in degraded mode (one or more services failed to remain up).

More details can be found in this Stack Overflow answer, in the Arch Linux wiki about SystemD timers and in the man pages for systemd.timer, but the gist of it is like this:

1. Setup the check service

/etc/systemd/system/checkwifi.service:

[Unit]
Description=...

[Service]
Type=oneshot
ExecSTart=/usr/bin/checkWiFi

2. Setup the timer that starts the check WiFi occasionally

/etc/systemd/system/checkwifi.timer:

[Unit]
Description=Timer for checkWifi
After=conWlan0.service

[Timer]
OnActiveSec=120

[Install]
WantedBy=timers.target

3. Enable and start the timer unit

systemctl enable --now checkwifi.timer

Note that when starting the timer, if the conWlan0.service hasn't started yet, systemctl would block and wait for the timer prerequirements to be fulfilled.

You must log in to answer this question.

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