Need help about cron job and its capability

I’m not a technical guy, but i need help.
I run dietpi on raspberry pi 02w and i use portainer.
I use 2 scripts,
script 1:

#!/bin/bash

# Ping Google
if ! ping -c 1 google.com &> /dev/null; then
    # If the ping fails, restart Docker
    systemctl restart portainer
fi

script 2:

#!/bin/bash

# Ping Google
ping -c 1 google.com > /dev/null 2>&1

# Check the exit status of the ping command
if [ $? -ne 0 ]; then
    # If ping failed, reboot the system
    reboot
fi

i created these using ai and placed script in cron minute and script 2nd in hourly cron

my doubt

  1. is script right ??
  2. does cron have capability of rebooting system??

Why do you wanna restart when you have no internet connection? Wouldn’t it be better to check why the connection gets lost and fix that, instead of the symptoms!?

Yes, cron can reboot the system. But maybe it’s better to restart docker and not portainer?!
Can you tell more about your actual problem? You loose internet connection from time to time?

It’s always better to use absolute paths in bash scripts, for example:

#!/bin/bash

# Ping Google
/usr/bin/ping -c 1 google.com > /dev/null 2>&1

# Check the exit status of the ping command
if [ $? -ne 0 ]; then
    # If ping failed, reboot the system
    /usr/sbin/reboot
fi
1 Like

thank you jappe

i seached about disconnection ,came to know raspberry pi will often loose its connection when using wirelessly

after few hours/days , i cant able to ssh into raspberry pi , but its connected to internet , and their is no way to gain acccess except rebooting my router ,after rebooting my router ,i again able to ssh into my pi

path to my script

/etc/cron.minutely/ping1
/etc/cron.hourly/reboot

i will change it to absolute path and change portainer to docker

where do you know this? And if you still have internet access how should ping to google change anything? This should be working if your internet access is fine.

why rebooting the whole router?

1 Like

im using a container thats connects to a particular site and site clearly see that pi still connected to internet

dont know but rebooting router helps me to get into pi everytime

i cant simply pull the power plug because it might corrupt my sd card ,so i reboot my router

its strange method ,but it works

anyway ,i soon move on to pi 5 ,because ,pi 02w is weak and cant handle plex,nextcloud,samba

Ok, if you want to troubleshoot this problem, I would recommend to enable full loggin (https://dietpi.com/docs/software/log_system/#full-logging) and check the logs when this happens again.

For me it’s seems like an issue with your local network, you can’t find the device anymore after some time in your LAN?

1 Like

yes ,my device completely disappear

i checked log

the error is due to ""brcmf_sdio_rxfail “”

i rechecked power source,connection ,cable nothing helps

but one user suggestion works ,my pi didnt create power from last 2 days

solution was to "over_voltage=2 " but since dietpi doest have 2 ,i choosed 3 ,and it works

still im trying to make my pi full proof so that in case if pi loose connection again ,script will reboot system

Did you already enable DietPi service who check WiFi connection?

sorry but i dont know about any service which check for wifi connection

i disabled ethernet and enabled wifi

Do you have an external drive connected by any chance?

yes,an external ssd

root@DietPi:~# systemctl enable dietpi-wifi-monitor.service
root@DietPi:~# systemctl status dietpi-wifi-monitor.service
● dietpi-wifi-monitor.service - DietPi-WiFi_Monitor
     Loaded: loaded (/etc/systemd/system/dietpi-wifi-monitor.service; enabled; p                                                                                                             reset: enabled)
     Active: active (running) since Sat 2025-04-05 19:46:21 BST; 2 days ago
   Main PID: 431 (dietpi-wifi-mon)
      Tasks: 3 (limit: 474)
     Memory: 20.5M
        CPU: 13min 22.718s
     CGroup: /system.slice/dietpi-wifi-monitor.service
             ├─   431 /bin/bash /var/lib/dietpi/services/dietpi-wifi-monitor.sh
             ├─947313 /sbin/wpa_supplicant -s -B -P /run/wpa_supplicant.wlan0.pi                                                                                                             d -i wlan0 -D nl80211,wext -c /etc/wpa_supplicant/wpa_supplicant.conf
             └─947318 dhclient -4 -v -i -pf /run/dhclient.wlan0.pid -lf /var/lib                                                                                                             /dhcp/dhclient.wlan0.leases -I -df /var/lib/dhcp/dhclient6.wlan0.leases wlan0

Apr 07 19:33:24 DietPi dietpi-wifi-monitor.sh[947318]: DHCPDISCOVER on wlan0 to                                                                                                              255.255.255.255 port 67 interval 10
Apr 07 19:33:25 DietPi dhclient[947318]: DHCPOFFER of 192.168.1.15 from 192.168.                                                                                                             1.1
Apr 07 19:33:25 DietPi dietpi-wifi-monitor.sh[947318]: DHCPOFFER of 192.168.1.15                                                                                                              from 192.168.1.1
Apr 07 19:33:25 DietPi dietpi-wifi-monitor.sh[947318]: DHCPREQUEST for 192.168.1                                                                                                             .15 on wlan0 to 255.255.255.255 port 67
Apr 07 19:33:25 DietPi dhclient[947318]: DHCPREQUEST for 192.168.1.15 on wlan0 t                                                                                                             o 255.255.255.255 port 67
Apr 07 19:33:25 DietPi dhclient[947318]: DHCPACK of 192.168.1.15 from 192.168.1.                                                                                                             1
Apr 07 19:33:25 DietPi dietpi-wifi-monitor.sh[947318]: DHCPACK of 192.168.1.15 f                                                                                                             rom 192.168.1.1
Apr 07 19:33:26 DietPi dhclient[947318]: bound to 192.168.1.15 -- renewal in 428                                                                                                             45 seconds.
Apr 07 19:33:26 DietPi dietpi-wifi-monitor.sh[947318]: bound to 192.168.1.15 --                                                                                                              renewal in 42845 seconds.
Apr 07 19:33:26 DietPi dietpi-wifi-monitor.sh[431]: [  OK  ] DietPi-WiFi_Monitor                                                                                                              | Completed
root@DietPi:~#

So the wifi monitor is running and you sill lose access.
Also that the DHCP lease is still active but you can not connect vai SSH sounds more like an power issue.

We always recommend to use a powered USB hub / powerd USB to SATA adapter or whatever you are using to connect the SSD to the device.
The USB ports of the RPi are not designed to deliver power to a device, Even the RPi 4 struggles with that (IDK about the RPi 5 tho).

1 Like

setting overvoltage to 3 seems to be working
im ordering ,raspberry pi usb hub 3 ,but will it able to backfeed power into pi ,or i still need separate power source

https://www.raspberrypi.com/documentation/accessories/usb-3-hub.html

The Raspberry Pi USB 3 Hub provides extra connectivity for your devices, extending one USB-A port into four. An optional external USB-C power input supports high-power peripherals.

Power transfer up to 900 mA (4.5 W); optional external USB-C power input provides up to 5V @ 3A for high-power downstream peripherals

So I would use a 15 watt phone charger and connect it to the hub via USB-C to be safe.

thats my plan ,i will order usb hub ,pi official power supply of 3a

i hope i only need 1 power supply and that goes into usb hub ,and hub will provide power to pi ( raspberry pi 0 2w have capability of getting power through data usb port also)

No, the hub will not power the pi.
The pi needs it’s own power supply. The hub can be powered via the pi, but since you have power issues I recommend a 2nd power supply jsut for the hub!

1 Like

See if anything here can help