0

I have made a systemd service in Raspberry Pi with Jessy OS and it is given by.

/etc/systemd/system/caportal.service

[Unit]
Description=captive portal automation

[Service]
Type=simple

ExecStart=/usr/bin/python /home/pi/.caportal/do.py
Restart=on-failure
RestartSec=5
StartLimitBurst=5
StartLimitInterval=2min

and /home/pi/.caportal/do.py is given by

import subprocess as cm
cm.check_output(['sudo', 'systemctl', 'daemon-reload'])
cm.check_output(['sudo', 'systemctl' ,'restart', 'networking.service'])
cm.check_output(['sudo', 'ifdown', 'wlan0'])
cm.check_output(['sudo', 'ifup', 'wlan0'])

Then modified the wpa_supplicant.conf

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="home2"
    psk="pass1234"
    priority=2
}

But when i start the service by sudo systemctl start caportal.service, do.py is not working as desired. That is it reconnect the wifi but disconnect when the service stops.

But if i run the python script do.py through the terminal, then the wifi is connected without any problem.

Can anyone help me to find this problem?

1

1) Your service is terminated (all process started by this service are killed) when your last script (python here) execution is finished.

Execute the python script into a terminal, your script exit with default value of 0 (process exit success code) after python file execution because you have not a forever loop (while). Service file is principaly for daemons control.

2) Remove this line : cm.check_output(['sudo', 'systemctl', 'daemon-reload']) and use ip command in place to ifup.

3) Another script already use wpa_supplicant (pidof wpa_supplicant)...

4) Use dmesg command or show in syslog file for any errors...

but that can be so many things.

If you have not disable the networking service at start-up (sudo systemctl disable networking.service) then here is what happens:

  • How to use ip command instead of ifup? – mcv Jun 13 at 4:44
  • ip link set wlan up and see my edited answer. – Ephemeral Jun 13 at 5:39
  • I am sorry I don't understand it. Can you please write the code ? what changes i need to do in do.py? – mcv Jun 13 at 10:17
  • what are you trying to do, I do not understand ? – Ephemeral Jun 13 at 13:53
  • Actually, do.py has some more codes which request a web page and it will repeat for every 6 hours. Some times internet is disconnected so I need to restart the wifi in the do.py script. But when I included the above piece of code in do.py, wlan0 is restarted and disconnected. – mcv Jun 14 at 8:48

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.