How to run a script before wifi is connected to a network?

2

I was trying to build a systemd service to run after the networking is up.

/etc/systemd/system/caportal.service

[Unit]
Description=captive portal automation

DefaultDependencies=no
After=local-fs.target


[Service]
Type=oneshot

ExecStart=/sbin/wpa_cli -a /home/pi/test.sh -B
ExecStop=/usr/bin/pkill -f /home/pi/test.sh

RemainAfterExit=yes

[Install]
WantedBy=network.target

But this service does not start at reboot. It start when manually started by sudo systemctl start caportal.service.

But after reboot, the status information of this service was given below.

● caportal.service - captive portal automation
   Loaded: loaded (/etc/systemd/system/caportal.service; enabled)
   Active: failed (Result: exit-code) since Thu 2019-04-11 10:59:32 UTC; 3min 3s ago
  Process: 287 ExecStart=/sbin/wpa_cli -a /home/pi/test.sh -B (code=exited, status=255)
 Main PID: 287 (code=exited, status=255)

Apr 11 10:59:32 raspberrypi systemd[1]: Starting captive portal automation...
Apr 11 10:59:32 raspberrypi wpa_cli[287]: Failed to connect to non-global ctrl_ifname: (null)  error: No such file or directory
Apr 11 10:59:32 raspberrypi systemd[1]: caportal.service: main process exited, code=exited, status=255/n/a
Apr 11 10:59:32 raspberrypi systemd[1]: Failed to start captive portal automation.
Apr 11 10:59:32 raspberrypi systemd[1]: Unit caportal.service entered failed state.

How to start the service at reboot before the network start?

mcv

Posted 2019-04-11T11:09:23.320

Reputation: 55

Answers

1

No, the status output says that your script did start successfully. However, it ran too early: the log shows that wpa_cli had been started, but exited because it couldn't connect to wpa_supplicant – which at this stage is not running yet.

The problem is that you're trying to insert the script between two events which the service manager considers as a single unit:

  1. You want it to run after wpa_supplicant has started (because wpa_cli wants to connect to it).
  2. You want it to run before wpa_supplicant has found and associated with a network.

There's no way you can achieve this with systemd configuration alone.

One of your options is to disable automatic connection but add an ExecStartPost= script which tells wpa_supplicant to begin connecting after wpa_cli has successfully started. Alternatively, keep automatic connection, but add an ExecStartPost= script which manually triggers the test.sh actions if necessary.

Further problems with your unit:

  • Type=oneshot tells systemd to expect that this is a short-lived unit that runs and exits, but wpa_cli -a obviously isn't that – according to its manual page, it runs as a daemon and needs Type=simple.

  • RemainAfterExit=yes is unnecessary because this isn't an 'oneshot' service. It'll only cause confusion – e.g. if wpa_cmd crashes, the service would still appear "active" even though it isn't running anymore, because this option told it so.

  • DefaultDependencies=no is unnecessary; that's way too early in terms of dependency ordering. Regular wpa_supplicant doesn't have this, so it's not needed here either; a simple Before= or After= is usually enough.

user1686

Posted 2019-04-11T11:09:23.320

Reputation: 283 655

Thanks @grawity. Can I start the script after wpa_supplicant.service? Also How to view the order in which systemd process each service? – mcv – 2019-04-12T04:00:05.723

Sure, [Unit] After=wpa_supplicant.service. But you can't guarantee that it'll run before or after wpa_supplicant has done something-or-other, only that it'll run after it has "started". Use systemctl list-dependencies --after, --before, and maybe systemd-analyze dot | dot -Tx11? – user1686 – 2019-04-12T04:20:24.500

yeah, I have already tried that After=wpa_supplicant.service. But it wasn't working. Now it shows the service as inactive – mcv – 2019-04-12T04:57:11.273

systemctl list-dependencies --after, this list not showing wpa_supplicant.service` – mcv – 2019-04-12T04:58:14.283

Did you tell it to list caportal.service? – user1686 – 2019-04-12T04:59:22.660

Yeah now i got the list, but the list shows that local-fs.target is run after wpa_supplicant.service. Then why wpa_cli is got error? – mcv – 2019-04-12T05:06:20.027

why the service is showing inactive and Process is showing success. What does it mean? But ps command shows test.sh is not running. – mcv – 2019-04-12T05:15:06.213