###################################################################
#
# this script check frpc status and wifi connection satus
# if not running, start it
# author: kzeng
# date: 2021-11-13
# */5 * * * * /home/pi/thrmp/check_and_run.sh
#
###################################################################

#  check wifi connection status
IP=`ifconfig |grep "192.168."`

echo $IP

if [ "$IP" != "" ]; then
	echo "LAN/WLAN ip address is ok ..."
else
	echo "not found ip address, restart networking ...."
	sudo /etc/init.d/networking restart
fi
###################################################################


# check frpc status
PIDS=`ps -ef |grep "frpc.ini" |grep -v grep | awk '{print $2}'`

echo $PIDS

if [ "$PIDS" != "" ]; then
	echo "frpc is running ..."
else
	echo "start frpc ..."
	/home/pi/frp/frpc -c /home/pi/frp/frpc.ini &
	#/data/bin/frp/frpc -c /data/bin/frp/frpc.ini &
fi
###################################################################


# check web service
WSIDS=`ps -ef |grep "run.py" |grep -v grep | awk '{print $2}'`

echo $WSIDS

if [ "$WSIDS" != "" ]; then
	echo "web server is running ..."
else
	echo "start web server ..."
	cd /home/pi/thrmp
	python3 run.py &
fi