You are not logged in.

#1 2019-09-02 07:53:51

Stefan_xyz
Member
Registered: 2019-09-01
Posts: 107

[SOLVED] connect to Wifi on startup

Okay so i want to automatically try to connect to my local (home) wifi on startup.
I wrote a script which get's executed at startup (via service)

The script:

#!/bin/bash

# connect to wifi
wpa_supplicant -B -c /etc/wpa_supplicant/wifi-name.conf -i wlp4s0
dhcpcd

This script works perfectly fine when I execute it with root during runtime.
But it doesn't work for some reason on startup. (I also tried to work around it with a sleep command which also didn't work)

Does anybody know maybe why it doesn't work?
A workaround would also be okay.

Thanks in advance!

Last edited by Stefan_xyz (2019-09-02 11:11:33)

Offline

#2 2019-09-02 07:56:12

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] connect to Wifi on startup

What you are trying to do is "a workaround". What you want is to enable a systemd service.


Moving to NC...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2019-09-02 08:20:34

Stefan_xyz
Member
Registered: 2019-09-01
Posts: 107

Re: [SOLVED] connect to Wifi on startup

jasonwryan wrote:

What you are trying to do is "a workaround". What you want is to enable a systemd service.

The script get's already called and executed during startup via a systemd service (self written). The service is also enabled.
I know this because I execute other commands also in this script.

Offline

#4 2019-09-02 08:37:21

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,417

Re: [SOLVED] connect to Wifi on startup

While that might be true it's unclear whether your service file does the right things. The packages in question do come with properly defined services that would not need you to try and reinvent the wheel: https://wiki.archlinux.org/index.php/WP … _(systemd)

If you actually want to fix this in your own hack (which you frankly probably shouldn't...) you might want to post your service file.

Last edited by V1del (2019-09-02 08:38:44)

Offline

#5 2019-09-02 08:50:20

Stefan_xyz
Member
Registered: 2019-09-01
Posts: 107

Re: [SOLVED] connect to Wifi on startup

V1del wrote:

While that might be true it's unclear whether your service file does the right things. The packages in question do come with properly defined services that would not need you to try and reinvent the wheel: https://wiki.archlinux.org/index.php/WP … _(systemd)

If you actually want to fix this in your own hack (which you frankly probably shouldn't...) you might want to post your service file.

The service file:

[Unit]
Description=Starts a script at startup

[Service]
ExecStart=/scripts/startup.sh

[Install]
WantedBy=multi-user.target

I enabled wpa_supplicant but it doesn't change anything.

Offline

#6 2019-09-02 08:57:28

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,417

Re: [SOLVED] connect to Wifi on startup

That's an insufficient service file for something that's supposed to handle network hardware, just stop this and use existing services, or at least read them to see what you're missing.

Stefan_xyz wrote:

I enabled wpa_supplicant but it doesn't change anything.

That's not an error message what exactly, which command,  did you enable, and did you enable a corresponding dhcpcd service too?

Which instructions did you follow to install Arch?

Last edited by V1del (2019-09-02 08:57:50)

Offline

#7 2019-09-02 09:14:15

Stefan_xyz
Member
Registered: 2019-09-01
Posts: 107

Re: [SOLVED] connect to Wifi on startup

V1del wrote:

That's an insufficient service file for something that's supposed to handle network hardware, just stop this and use existing services, or at least read them to see what you're missing.

Hmm i don't quite see how this is inusfficient since it executes my script at startup and all further commands get executed in this script so I'm perfectly fine with it. (And the service execute the script!!!)


V1del wrote:

That's not an error message what exactly, which command,  did you enable, and did you enable a corresponding dhcpcd service too?

As you see in the script posted i set of an corresponding dhcpcd service.
I logged the outputs:

dhcpd "error" (no standard output):

dev: loaded udev
no interfaces have a carrier
forked to background, child pid 923

wpa_supplicant log (no error):

Successfully initialized wpa_supplicant

But the script works fine during runtime. It also get's executed during startup. It just doesn't work during startup (or it works but the settings get reset)


V1del wrote:

Which instructions did you follow to install Arch?

I followed the standard installtation guide

Offline

#8 2019-09-02 09:27:21

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,417

Re: [SOLVED] connect to Wifi on startup

Your script runs too soon, without any attempt to wait for network hardware to be available. You will at the very least want a Wants=network.target and After=network.target

But this is likely to break in subtle ways with things that expect the proper systemd services to be invoked, you should simply enable these directly and not try to reinvent the ordering wheel systemd cleanly solves.

Which is what I refered to when I told you to tell me the exact command, because the section i linked to lists several.

Also FWIW

systemctl status startup.service #Or whatever the actual name is

Offline

#9 2019-09-02 10:13:44

Stefan_xyz
Member
Registered: 2019-09-01
Posts: 107

Re: [SOLVED] connect to Wifi on startup

V1del wrote:

Your script runs too soon, without any attempt to wait for network hardware to be available. You will at the very least want a Wants=network.target and After=network.target

I implemented a sleep command (used them before but also didn't work) which is ends well after i logged into my user. I get again the same ("error") messages.


V1del wrote:

But this is likely to break in subtle ways with things that expect the proper systemd services to be invoked, you should simply enable these directly and not try to reinvent the ordering wheel systemd cleanly solves.

In what way breaking? (Does this also apply to my "sleep-solution"?


V1del wrote:

Which is what I refered to when I told you to tell me the exact command, because the section i linked to lists several.

I don't understand what you mean with that. Do you need more information? Or what?

Offline

#10 2019-09-02 10:41:49

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,417

Re: [SOLVED] connect to Wifi on startup

Stefan_xyz wrote:
V1del wrote:

Your script runs too soon, without any attempt to wait for network hardware to be available. You will at the very least want a Wants=network.target and After=network.target

I implemented a sleep command (used them before but also didn't work) which is ends well after i logged into my user. I get again the same ("error") messages.

I'd assume the actual error here being that you let dhcpcd fork off, systemd runs the service, your bash shell exits successfully, and systemd cleans up any resulting child processes. If you want to actually work with forking services you'd need to define type=forking in your service file. Just as the proper and already existing systemd services for dhcpcd do.

In what way breaking? (Does this also apply to my "sleep-solution"?

There might be services that utilize information system services supply as to when the connection is up or similar, which they can't ask for, since logically you aren't running the actual service files. Will in this particular case likely not be as problematic as it could be, but still.

That said, ignore your own service file, follow the link I posted (if you want to explicitly enable on an interface make sure you rename your config file to the outlined template, enable the relevant dhcpcd service on the chosen interface) post the commands you are using to accomplish this. Should it still not work on reboot post the complete output of

sudo journalctl -b > journal.log
systemctl list-unit-files --state enabled
#If you still intend to use that interface, these should be your enabled services
systemctl status wpa_supplicant@wlp4s0
systemctl status dhcpcd@wlp4s0

Last edited by V1del (2019-09-02 10:47:31)

Offline

#11 2019-09-02 11:11:04

Stefan_xyz
Member
Registered: 2019-09-01
Posts: 107

Re: [SOLVED] connect to Wifi on startup

V1del wrote:

I'd assume the actual error here being that you let dhcpcd fork off, systemd runs the service, your bash shell exits successfully, and systemd cleans up any resulting child processes. If you want to actually work with forking services you'd need to define type=forking in your service file. Just as the proper and already existing systemd services for dhcpcd do.

There might be services that utilize information system services supply as to when the connection is up or similar, which they can't ask for, since logically you aren't running the actual service files. Will in this particular case likely not be as problematic as it could be, but still.

Okay i see thanks for the explanation

V1del wrote:

That said, ignore your own service file, follow the link I posted (if you want to explicitly enable on an interface make sure you rename your config file to the outlined template, enable the relevant dhcpcd service on the chosen interface) post the commands you are using to accomplish this. Should it still not work on reboot post the complete output of

Okay yeah it worked. THANKS!

For those interested:

I enabled the wpa_supplicant@interface.service and renamed my wpa_supplicant file (which i named "SSID.conf") to "wpa_supplicant-interface.conf
I also enabled an dhcpcd@interface.servcie to get an IP-Addr.

For me Information: here . Source from V1del

Offline

Board footer

Powered by FluxBB