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?

Ingo
  • 42,961
  • 20
  • 87
  • 207
mcv
  • 73
  • 3
  • 9

1 Answers1

-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

mcv
  • 73
  • 3
  • 9