Scripts crontab -e trouble

Hello,
I'm using the following scripts to ensure connectivity but is it possible those will end up in a boot loop or similar if there is no internet connection?
at crontab -e I inserted a job all 30 min to start 4G-restart-interface.sh

#---------------root@OpenWrt:~# cat 4G-keep-alive.sh--------------------------
#------------------------------------------------------------------------------------

#!/bin/ash
# The main script file. I recommend putting into cron, e.g.:
# */2 * * * * /root/4G-keep-alive.sh
 
DIR=$( cd $(dirname $0) ; pwd -P )
LOG_FILE="$DIR/log-4G.txt"
 
OFFLINE_COUNT=$(cat $LOG_FILE | tail -4 | grep OFFLINE | wc -l)
OFFLINE_COUNT_TRESHOLD=4
 
SH_DNS_TESTS="$DIR/4G-dns-test.sh"
SH_RESTART_INTERFACE="$DIR/4G-restart-interface.sh"
SH_RESTART_ROUTER="$DIR/restart-router.sh"
 
LINES_MAX=11000
LINES_MIN=6000
LINES_COUNT=$(wc -l $LOG_FILE | awk '{print $1}')
 
# if the log files gets huge, strip it, keep last LINES_MIN lines
if [[ "$LINES_COUNT" -ge "$LINES_MAX" ]]; then
   echo "$(tail -$LINES_MIN $LOG_FILE)" > $LOG_FILE
fi
 
# DNS test, it's result defines the ONLINE/OFFLINE state
`$SH_DNS_TESTS`
 
if [ $? -eq 1 ]; then
   echo "Ooops, LTE network is down!"
   echo "$(date) OFFLINE > RESTARTING INTERFACE" >> $LOG_FILE
 
   if [[ "$OFFLINE_COUNT" -ge "$OFFLINE_COUNT_TRESHOLD" ]]; then
      echo ">> Restarting router.." >> $LOG_FILE
      $SH_RESTART_ROUTER
   else
      echo ">> Restarting LTE interface.." >> $LOG_FILE
      $SH_RESTART_INTERFACE
   fi
else
   echo "LTE is okay!"
   echo "$(date) ONLINE" >> $LOG_FILE
fi
#------------------root@OpenWrt:~# cat 4G-dns-test.sh-----------------------------
#------------------------------------------------------------------------------------

#!/bin/ash
# This file is responsible for DNS check. The return value of its process
# determines the ONLINE/OFFLINE state.
 
IP_TO_PING=8.8.8.8
PACKET_COUNT=4
 
ONLINE=0
 
for i in `seq 1 $PACKET_COUNT`;
        do
                nc -G 2 -z $IP_TO_PING 53
                RETVAL=$?
                                if [ $RETVAL -eq 0 ]; then
                                        ONLINE=1
                                fi
        done
 
if [ $ONLINE -eq 1 ]; then
    # ONLINE
    exit 0
else
    # OFFLINE
    exit 1
fi
#------------------------------------------------------------------------------------
#-----------------root@OpenWrt:~# cat 4G-restart-interface.sh---------------------------
#------------------------------------------------------------------------------------

#!/bin/ash
# This file is responsible for restarting the network interface.
# Should be run once OFFLINE state is detected.
 
INTERFACE="4G"
 
# syslog entry
logger -s "INTERNET KEEP ALIVE SYSTEM: Restarting the LTE interface."
 
echo "SH RESTART IFACE DOWN"
ifdown $INTERFACE
 
sleep 2
 
echo "SH RESTART IFACE UP"
ifup $INTERFACE
#sleep 4
#------------------------------------------------------------------------------------
#------------------root@OpenWrt:~# cat restart-router.sh-----------------------------
#------------------------------------------------------------------------------------

#!/bin/ash
# This file is responsible for restarting the router using reboot command.
# There is a stratery to write few lines into the log, so that grepping last lines
# returns less occurences of word OFFLINE. Too many occurences actually run this script.
 
DIR=$( cd $(dirname $0) ; pwd -P )
LOG_FILE="$DIR/log.txt"
 
echo "$(date) > TOO MANY OFFLINE TRYOUTS" >> $LOG_FILE
echo "$(date) > GOING TO REBOOT NOW" >> $LOG_FILE
echo "$(date) > NOW!" >> $LOG_FILE
echo "$(date) > SORRY FOR ANY INCONVENIENCE." >> $LOG_FILE
 
echo "SH REBOOT"
reboot

Why didn't you show this?

Yes, a boot loop is possible if a cron launched script executes reboot within the same minute it's ran. See: https://openwrt.org/docs/guide-user/base-system/cron#periodic_reboot

  • Add a sleep 70 command - this ensures the script doesn't run/complete in the same minute
  • Additionally, use the touch command to alter the timestamp of a file in the directory /etc - this ensures that the devices has a boot time reference that is in the future - past the time in the cron task
1 Like

By doing this, you just restart the 4G interface every 30 minutes.
The right approach would be to create a cron job for the main script (4G-keep-alive.sh) as suggested:

The purpose of the script is to check the Internet connectivity every 2 minutes and restart the 4G interface if there is no connection.

If there is still no internet access after the 4th restart of the 4G interface (at the 10th minute), it will restart the router.

However, you should not use the script (at least not in this version) for several reasons:

This will save the log file in the script directory and wear out the flash memory.

The standard BusyBox version of netcat does not support these options and it will return an error (1) on each check, so the router will restart every 10 minutes.

And last but not least, it is unnecessarily complicated

2 Likes

Ok sounds reasonable.

But what/how else would it be done to achieved the following:

A) no wireguard tunnel (checked by pinging wg-server) check 4G
B) no 4G, restart 4G interface otherwise restart wireguard interface.

C) have this router always accessible by my home network since its standalone, remote and a 10 houra drive away from anyone i know so i really need a reliable system, kinda like a dpace probe ^^

#!/bin/sh

IP1=10.9.8.1    # Wireguard server address
IP2=8.8.8.8 	# Address to check the 4G connection 
IF1=wg0         # Logical name of the wireguard interface
IF2=4G  		# Logical name of the 4G interface

if ! ping -c 3 -W 1 $IP1 >/dev/null; then
   
   if ! ping -c 3 -W 1 $IP2 >/dev/null; then
      ifup $IF2
   else
      ifup $IF1
   fi

else
   exit 0
fi

Edit the variables according to your needs and create a cron job.
I have never used 4G services. If you think it's better, replace ifup $IF2
with ifdown $IF2; sleep 2; ifup $IF2

2 Likes

Nice looks very clean, so this gets rid off all the problems u mentioned?

How would i add 2 log files one for wireguard check and second one for 4g check with a timestamp so later on i can see how often or what times it reatarted and all that

May i add a system reboot as a cron job so the unattended device will restart like once a week or something?
Did not quite understand the problem u mentioned about reboot-timestamp-cron job

# Reboot at 4:30am every day
# Note: To avoid infinite reboot loop, wait 70 seconds
# and touch a file in /etc so clock will be set
# properly to 4:31 on reboot before cron starts.
30 4 * * * sleep 70 && touch /etc/banner && reboot

@lleachii posted that link to the Wiki.

Each line is a separate task written in the specification:

* * * * * command to execute
- - - - -
| | | | |
| | | | ----- Day of week (0 - 6) (Sunday =0)
| | | ------- Month (1 - 12)
| | --------- Day (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

I wouldn't recommend an unattended cron reboot since your hours away from support if needed.

1 Like

Better use the standard system log.

if ! ping -c 3 -W 1 $IP2 >/dev/null; then
      ifup $IF2
      logger -t wanlog "4G restarted";
   else
      ifup $IF1
      logger -t wanlog "wireguard restarted";
   fi

 logread -e wanlog
1 Like

If your problem is solved, please consider marking this topic as [Solved]. See How to mark a topic as [Solved] for a short how-to.

i

I will do so, wasn't able to try, still "fighting" the 4G connectivity in general