Easytether startup and check script

I'll give a little background first. I'm a truck driver, I use easytether to provide internet access to all of my devices in the truck, not only for work, but also for my side projects and my schooling. I originally started with using a Hak5 WiFi Pineapple Nano, which recently went belly up. So I went ahead and bought a new Version 5 TP-Link Archer C7 AC1750, installed OpenWRT and installed easytether. Through my experience with the Pineapple, and my subsequent experience now with the TP-Link, easytether likes to have occasional issues, to which I'm accustomed to at this point. So I wrote a script that would run at start.

#exec creates and writes all of the outputs of the commands in the
#script to a log file zmodlog.txt
exec &> /root/zmodlog.txt

#easytether-usb is started
date +"%D %T"
/usr/bin/easytether-usb

#to ensure that all of the commands in the while loop execute properly
#an update to all packages on the router is executed through opkg
opkg update

#the next two lines echo that the script will sleep for one hour
#then the script will sleep for one hour
date +"%D %T"
echo "Sleeping for one hour"
sleep 3600

#Beginning of the while loop
while true
	do
		PID=`ps | grep easytether-usb | grep -v grep | cut -d" " -f2``
		kill -9 $PID
		/usr/bin/easytether-usb
		date +"%D %T"
		echo "Sleeping for one hour"
		sleep 3600
	done


As I've come to find, most of the time, this works. However, once in a while the starting of easytether doesn't work, and I have to manually SSH into the AP and restart it. I've tried to remedy this by adding two crontab jobs, one at 5am, and one at 9pm, both rebooting the AP. This has had some success, but the restart of easytether still fails at least once a day. I was thinking about nesting an if statement within the while loop. Unfortunately, I'm uncertain of how I should go about it. I've written a rudimentary test script.

A=`ps | grep easytether-usb | grep -v grep | cut -d" " -f12`"

if [ "$A" = "/usr/bin/easytether-usb" ]
then

echo "Easytether is running"

else

echo "Easytether was not running
/usr/bin/easytether-usb

Fi

Now, I'd like to run that at the end of the while loop with a fail counter, or something similar to that with a fail counter. I was thinking of doing something more like adding a few static variables with failure outputs of easytether, so that if the output of easytether matched the variable it would try again in a loop until it's tried three times. I'm unsure how to properly do this, any input would be appreciated.

Perhaps you want to run /usr/bin/easytether-usb as a procd init script which can provide fault tolerance with respawn.

2 Likes

I had actually tried that when I first got the AP, I had gotten the same results. About two hours after the process started, my connection to the internet from my multiple devices would drop completely, even though the process was still running and the phone it was connected to showed that the AP was still connected.

2 Likes

After looking at nesting, considering all of my options, and a little help from my father, we've decided on this.

#exec creates and writes all of the outputs of the commands in the
#script to a log file zmodlog.txt
exec &> /root/zmodlog.txt

#easytether-usb is started

#Timestamp on log
date +"%D %T"

/usr/bin/easytether-usb

#to ensure that all of the commands in the while loop execute properly
#an update to all packages on the router is executed through opkg
opkg update

IPID=`ps | grep easytether-usb | grep -v grep | cut -d" " -f2`

#Timestamp on log
date +"%D %T"

#Sleep counter
echo "Easytether PID is $IPID"
echo "Sleeping for one hour"
sleep 3300

#Beginning of the while loop
while true
	do
		#Getting current process ID of easytether and assigning it to a variable
		PID=`ps | grep easytether-usb | grep -v grep | cut -d" " -f2`
		
		echo="Initial PID was $IPID, current PID is $PID. Killing Easytether now."
		kill -9 $PID

		#Restarting Easytether
		/usr/bin/easytether-usb
        PID=`ps | grep easytether-usb | grep -v grep | cut -d" " -f2`

			#While loop to check the restart of easytether, if $PID has no
			#value it will run until a value exists
			while [ -z $PID ]
				do
					/usr/bin/easytether-usb
					sleep 5
					PID=`ps | grep easytether-usb | grep -v grep | cut -d" " -f2`
				done

		#Timestamp on log
		date +"%D %T"
		echo "Sleeping for one hour"
		sleep 3300
	done

Obviously, there have been some other changes besides just the nesting. I've shortened sleep time from 60 minutes to 55 minutes, as well as further commenting in the script

You can also restart the service with cron based on internet connectivity status:

ping -w 3 openwrt.org &>/dev/null || /etc/init.d/easytether restart

And don't forget to disable negative DNS caching if you use domain names:

uci set dhcp.@dnsmasq[0].nonegcache="1"
uci commit dhcp
/etc/init.d/dnsmasq restart
2 Likes

I'll definitely consider this if what I have doesn't work, I'm still thinking it might need a fail counter, just in case a reboot may be needed to get easytether running again. However, it's not broken, so I'm not going to try fixing it. (Thank you fatherly advice.) Wish I had listened to that before I had tried adding a VPN tunnel and having to redo this entire project all over from scratch.

1 Like