0

My rpi is connected to a captive portal hotspot. I have prepared a python login script to login to the captive portal and it was working fine. But sometimes the wifi is getting disconnected and at that time i need to restart the wifi. Hence I have prepared a systemd service called restartwifi.service and it is given as follows. When I need to restart the service, I will start the service. But it won't help me.

[Unit]
Description=captive portal automation

[Service]
Type=simple

ExecStart=/home/pi/.caportal/do.sh

The script do.sh is given as

#!/bin/bash

ifdown wlan0 
ifup wlan0 

When i start the service, ifdown and ifup are working fine but after the complete execution, if I run ifconfig, it will give the following output.

eth0      Link encap:Ethernet  HWaddr b8:27:eb:57:b8:d9  
          inet addr:192.168.42.1  Bcast:192.168.42.255  Mask:255.255.255.0
          inet6 addr: fe80::d490:e6a8:b4dc:4c65/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10856 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10767 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:976358 (953.4 KiB)  TX bytes:3940237 (3.7 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: fe80::e342:200d:c1ad:eda1/64 Scope:Link
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:690 errors:0 dropped:0 overruns:0 frame:0
          TX packets:690 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:63416 (61.9 KiB)  TX bytes:63416 (61.9 KiB)

and the systemctl status restartwifi is look like below.

   Loaded: loaded (/etc/systemd/system/restartwifi.service; static)
   Active: inactive (dead)

Aug 27 15:45:38 raspberrypi dhclient[8349]: DHCPREQUEST on wlan0 to 255.255.255.255 port 67
Aug 27 15:45:38 raspberrypi dhclient[8349]: DHCPOFFER from 192.168.1.1
Aug 27 15:45:38 raspberrypi do.sh[7957]: DHCPREQUEST on wlan0 to 255.255.255.255 port 67
Aug 27 15:45:38 raspberrypi do.sh[7957]: DHCPOFFER from 192.168.1.1
Aug 27 15:45:38 raspberrypi dhclient[8349]: DHCPACK from 192.168.1.1
Aug 27 15:45:38 raspberrypi do.sh[7957]: DHCPACK from 192.168.1.1
Aug 27 15:45:38 raspberrypi dhclient[8349]: bound to 192.168.1.4 -- renewal in 36059 seconds.
Aug 27 15:45:38 raspberrypi do.sh[7957]: bound to 192.168.1.4 -- renewal in 36059 seconds.
Aug 27 15:45:38 raspberrypi wpa_supplicant[8345]: wlan0: CTRL-EVENT-DISCONNECTED bssid=58:d7:59:38:58:f4 reason=3 locally_generated=1
Aug 27 15:45:39 raspberrypi wpa_supplicant[8345]: wlan0: CTRL-EVENT-TERMINATING

But all working fine if I run the script do.sh alone in the terminal. What happen when I run the script in systemd service?

  • 1
    Your service does not have any dependency when it should run. So it starts on boot up as one of the first services ever. It will not find any initialized networking. Then it finishes after a short time (after doing ifdown/ifup). You should use Type=oneshot instead of default Type=simple. Why running the script just one time on early boot up? It doesn't make sense. – Ingo Aug 27 at 12:33
  • @Ingo I am not running the service at boot. I only start the service when i need to restart the wifi. – mcv Aug 28 at 5:59
  • @Milliways Why you are always doing a negative approach. Sometimes, wifi is not connecting or sometimes we need to modify the configuration and need to restart the wifi. – mcv Aug 28 at 7:03
  • @Ingo I have tried by Type=oneshot but nothing changed.. I got same result. Wifi is getting disconnected on service termination. – mcv Aug 28 at 7:06
  • You don't enable the service? You shouldn't cripple the output of commands. Why do yo need a service then? Just execute /home/pi/.caportal/do.sh and it is done. – Ingo Aug 28 at 7:16
-1

Modified the .service file as follows

[Unit]
Description=captive portal automation

[Service]
Type=simple
KillMode=process

ExecStart=/home/pi/.caportal/do.sh

That's it!

Thanks for the comments

  • I do not understand what you are doing. What is the child process? A two liner bash script that also terminates after a second? – Ingo Aug 29 at 10:43
  • Yup that's confusion. But the problem is solved now. From the status in my question wlan0: CTRL-EVENT-DISCONNECTED bssid=58:d7:59:38:58:f4 reason=3 locally_generated=1, we can understand the wifi is get disconnected when the service terminates. But after giving KillMode, its not like that and the wifi is still connected. – mcv Aug 29 at 11:29

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.