0

I have a set of tasks that I run on a schedule, but it requires network. I have a raspberry pi 4b

I have the /etc/wpa_supplicant/wpa_supplicant.conf setup like this:

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

network={
    ssid="mynetworkname"
    psk="mynetworkpassword"
    key_mgmt=WPA-PSK
}

My /etc/network/interfaces looks like this:

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

I'm having trouble finding a simple set of commands inside a .sh script where I can just reconnect to wifi if its not connected. I tried this:

#!/bin/bash

ping -c2 192.168.1.1

if [ $? != 0 ];
then
    echo "Network connection not healthy";
    ifup wlan0 up;
    #I also tried the above with ifconfig

else
    echo "Network connection healthy";
fi

I need help on what my wpa_supplicant.conf, and network/interfaces and anything else should look like to be able to do this. When I try the above with the default network/interface I get this error:

Network connection not healthy
ifup: unknown interface wlan0
ifup: unknown interface up

When I try with one of the below recommended contents for that file I get:

Network connection not healthy
wpa_supplicant: /sbin/wpa_supplicant daemon failed to start
run-parts: /etc/network/if-pre-up.d/wpasupplicant exited with return code 1
ifup: failed to bring up wlan0
ifup: unknown interface up

I've checked at least these and others not listed:
How to automatically reconnect WiFi?
https://francisuniverse.wordpress.com/2018/01/07/how-to-automatically-reconnect-raspberry-pi-to-wifi/
http://www.d3noob.org/2018/04/reconnecting-raspberry-pi-to-wireless.html
https://unix.stackexchange.com/questions/555325/disconnect-and-reconnect-wifi-via-command-line
https://ericplayground.com/2017/11/06/set-up-wifi-through-the-command-line-terminal-on-raspberry-pi/
https://tech.scargill.net/pi-zero-wi-fi-automatic-reconnect/

None of the above worked or at least didn't give me enough of the picture to make it work.

  • **Why** do you want to do anything? - `dhcpcd` already does this. Using obsolete Debian networking tools won't help. – Milliways Jan 23 '22 at 02:10
  • Hmm, then is it possible something is wrong with my dhcpcd service? I don't **want** to do anything haha. But my raspberry pi keeps losing network connection suddenly and only physically rebooting it auto-connects. – Jamalan Jan 24 '22 at 00:10

0 Answers0