Auto restart if no network bash script

Amishkaz

Cadet
Joined
Jul 26, 2023
Messages
5
!!Warning this could leed to boot looping if not set properly!!
I just want to post this so that someone might find it usefull i am a noobie so this is simple and easy to understand

Login or switch to shell

First create a bash script file using a command editor like nano
nano /bin/script/reboot.sh

then insert the bash code
#!/usr/bin/env bash # Function to check network connectivity check_network() { if /sbin/ping -q -c 1 192.168.1.1 &> /dev/null; then return 0 # Network is available else return 1 # Network is not available fi } # Main script logic if check_network; then echo "Network is available" else echo "Network is not available" shutdown -r now fi

Then using the chmod command give excution permission to the script
chmod 777 /bin/script/network.sh
or
chmod +x /bin/script/network.sh
you can check the script by runing it using the command
bash /bin/script/network.sh

Then login to the webUI and go to Tasks > Init/Shutdown Scripts and click ADD

Change the settings as shown below
1690370340412.png


Use the command
bash /bin/script/network.sh

NOTE
if you are planning to use other commands remeber to use the full path for the command
ex: instead of using "ping" use "/sbin/ping"

the full path to a command can be obtained by using "where"
ex:
where ping
 
Joined
Jun 2, 2019
Messages
591
Welcome!

Rather than using a sledgehammer, wouldn't it be better to figure out why the network is going down that you seem to think requires a reboot?
 
Last edited:

Amishkaz

Cadet
Joined
Jul 26, 2023
Messages
5
Welcome!

Rather than using a sledgehammer, wouldn't it be better to figure out why the network is going down that you seem to think requires a reboot?
The actual reason is,
i am using a normal desktop PC as the truenas server and due to a motherboard issue i replace the motherboard
When using wake on LAN to turn on the PC the network adapter doesnt work
but after restarting using reset button or from terminal it works
That is the reason i am using a script to check for network connectivity at boot and restart if no network is detected
 
Joined
Oct 22, 2019
Messages
3,641
Rebooting an entire server because of a failed ping shortly after bootup? That's way too much for a problem that should be addressed elsehwere. Heck, why even reboot the entire server? Why not just restart the network interface?

But still, no one should be doing this. It's not normal for your NAS server to randomly have a non-working network interface.
 
Joined
Jun 2, 2019
Messages
591
When using wake on LAN to turn on the PC the network adapter doesnt work
Replacing or adding a separate NIC is not an option?
 

Amishkaz

Cadet
Joined
Jul 26, 2023
Messages
5
Rebooting an entire server because of a failed ping shortly after bootup? That's way too much for a problem that should be addressed elsehwere. Heck, why even reboot the entire server? Why not just restart the network interface?

But still, no one should be doing this. It's not normal for your NAS server to randomly have a non-working network interface.
I agree 100% i posted or started this thread mainly to give a guidline on how to run a custom init script cause personally i wasnt able to find any working or easy instructions
 
Top