Code:
#!/bin/sh
#
# v0.41 by meigrafd
#
# Changelog:
#
# v0.41: added '-i 1' to check_respond as NCINTERVAL
# changed script syntax for cronjob to: check auto
# added misc stuff like script-config-checks etc...
#
# v0.40: added RESPONDCHECK
# added ENABLECOLORS
# fixed start_cam
#
#
# Recommended HowTo:
#
# Add a new shell user and install there your CCcam:
#
# useradd -m -s/bin/bash CS
# su - CS
# mkdir CCcam ; mkdir CCcam/bin ; mkdir CCcam/logs ; mkdir CCcam/etc
# cd CCcam ; ls -la
#
# Copy your files as followed: (inc. this script as: cccam)
#
# CCcam/
# cccam
#
# CCcam/etc:
# CCcam.cfg CCcam.channelinfo CCcam.providers SoftCam.Key AutoRoll.Key constant.cw
#
# CCcam/bin:
# CCcam.x86
#
# Change the following in your CCcam.cfg:
#
# TELNETINFO USERNAME :
# TELNETINFO PASSWORD :
# TELNETINFO LISTEN PORT : 16000
# SOFTKEY FILE : /home/CS/CCcam/etc/SoftCam.Key
# AUTOROLL FILE : /home/CS/CCcam/etc/AutoRoll.Key
# STATIC CW FILE : /home/CS/CCcam/etc/constant.cw
# CAID PRIO FILE : /home/CS/CCcam/etc/CCcam.prio
# PROVIDERINFO FILE : /home/CS/CCcam/etc/CCcam.providers
# CHANNELINFO FILE : /home/CS/CCcam/etc/CCcam.channelinfo
# LOG WARNINGS : /home/CS/CCcam/logs/warnings.log
#
# Add some cronjob entry so CCcam gets started on reboot and gets checked every minute:
#
# echo "@reboot $HOME/CCcam/cccam start >/dev/null 2>&1" >> $HOME/.cron
# echo "* * * * * $HOME/CCcam/cccam check auto >/dev/null 2>&1" >> $HOME/.cron
# crontab $HOME/.cron
#
# ...Feel free to change the paths...
# ...which depends on your receiver/system/image...
#
#
### CONFIG - START
# Enable CCcam responding check ? [yes/no]
# NOTE: if set to 'no' you can ignore the TELNETINFO-IP and TELNETINFO-PORT setting...
RESPONDCHECK="yes"
# Temp file for respond-checks. gets deleted after...
CHECKFILE="/tmp/cccamtest.log"
# TELNETINFO-IP of your CCcam-Server for respond-checks (normaly: localhost/127.0.0.1)
IP="127.0.0.1"
# TELNETINFO-PORT of your CCcam-Server for respond-checks [CCcam-default: 16000]
PORT="16000"
# Enable colored output-lines on shell? [yes/no]
ENABLECOLORS="yes"
# Path where the CCcam binary lies [CCcam-default: /var/emu]
CCcamPATH="/home/CS/CCcam/bin"
# Name of the CCcam binary [CCcam-default: cccam]
# NOTE: if you want to run more than one CCcam-server:
# rename the binary (eg: CCcam.2.x86) , make a copy of this cccam-script and reconfigure it...
CCcamBIN="CCcam.211.x86"
# Path to save the CCcam logs to [CCcam-default: /tmp]
CCcamPLOG="/home/CS/CCcam/logs"
# Inside $CCcamPLOG we save the crontabed checks to which file?
CCcamCLOG="$CCcamPLOG/checklog.CCcam"
# Complete path/to/file of your CCcam.cfg [CCcam-default: /var/etc/CCcam.cfg]
CCcamCFG="/home/CS/CCcam/etc/CCcam.cfg"
# Options to start CCcam with [CCcam-default: <none>]
# NOTE: you MUST set "-C /path/to/CCcam.cfg" if its NOT CCcam-default!
CCcamOPTIONS="-C $CCcamCFG"
# delay interval for lines sent (for responding check) [default: -i 1]
# NOTE: if your CCcam-server runs on an 1xChip dbox its possible that the
# respond-check tooks too long for this script. Than you need to set
# this to '-i 3' or '-i 4'... it waits longer befor quitting the respond-check
NCINTERVAL="-i 2"
### CONFIG - END
# -------------------------------------------------------------- #
# >>> >> > DO NOT MESS WiTH ANYTHiNG BELOW THiS LiNE! < << <<< #
# -------------------------------------------------------------- #
if [ "$ENABLECOLORS" = "yes" ]; then
ROT=`tput setaf 1`
GRUEN=`tput setaf 2`
BOLD=`tput bold`
NORM='tput op && tput sgr0'
else
ROT="" ; GRUEN="" ; BOLD="" ; NORM=""
fi
if [ ! -d "$CCcamPLOG" ]; then
echo $ROT "ERROR: Directory $CCcamPLOG does not exist!"
exit 0
fi
if [ ! -e $CCcamCLOG ]; then
touch $CCcamCLOG
fi
if [ ! -w "$CCcamCLOG" ]; then
echo $ROT "ERROR: Can not write to $CCcamCLOG - Check paths and perms!"
exit 0
fi
DATE=`date`
# This method starts CCcam
start_cam ()
{
if ps x |grep -v grep |grep -c $CCcamBIN >/dev/null
then
echo $ROT "ERROR: $CCcamBIN still runs with pid: $(pidof $CCcamBIN)"
$NORM
else
cd $CCcamPATH
./$CCcamBIN $CCcamOPTIONS > $CCcamPLOG/log.CCcam
sleep 1
echo $GRUEN "Successfully started CCcam with pid: $(pidof $CCcamBIN)"
$NORM
fi
}
# This method stops CCcam
stop_cam ()
{
PiD=$(pidof $CCcamBIN)
if [ -z "$PiD" ]; then
echo $ROT "ERROR: Cant see $CCcamBIN to stop..."
$NORM
else
echo $GRUEN "Stopping CCcam with pid: $PiD"
$NORM
kill -9 $PiD
fi
}
# This method checks for running process and starts if not...
check_run ()
{
if ps x |grep -v grep |grep -c $CCcamBIN >/dev/null
then
echo $GRUEN "Seems CCcam still runs with pid: $(pidof $CCcamBIN)"
$NORM
if [ "$RESPONDCHECK" = "yes" ]; then
check_respond
fi
else
echo $ROT "Cant see your CCcam! Starting: $CCcamBIN"
$NORM
start_cam
fi
}
# This method checks if CCcam still responding like it should
check_respond ()
{
process=`ps auxwww | grep $CCcamBIN | grep -v grep | awk '{print $1}'`
if [ -z "$process" ]; then
echo " BAD - Couldn't find $CCcamBIN running. Restarting!"
echo "["$DATE"] BAD - Couldn't find $CCcamBIN running. Restarting!" >> $CCcamCLOG
start_cam
else
echo "CCcam process is present, now checking if CCcam is frozen"
# minimize Logfile size
LASTLINECHECK=`tail -n1 $CCcamCLOG | grep "now checking if CCcam is frozen"`
if [ -z "$LASTLINECHECK" ]; then
echo "CCcam process is present, now checking if CCcam is frozen" >> $CCcamCLOG
fi
sleep 2
echo info|nc $NCINTERVAL $IP $PORT > $CHECKFILE
CHECKED=$(ls -s $CHECKFILE)
if [ "$CHECKED" = "0" ]; then
echo " BAD - CCcam is frozen, restarting CCcam: $CCcamBIN"
echo "["$DATE"] BAD - CCcam is frozen, restarting CCcam" >> $CCcamCLOG
stop_cam
sleep 2
start_cam
else
echo " OK - CCcam is responding like it should!"
# output responding to shell if its NOT triggered: check auto
if [ -z "$3" ]; then
echo "CCcam responding:"
echo $(cat $CHECKFILE)
fi
# minimize Logfile size
LASTLINECHECK=`tail -n1 $CCcamCLOG | grep "CCcam is responding like it should"`
if [ -z "$LASTLINECHECK" ]; then
echo "["$DATE"] OK - CCcam is responding like it should" >> $CCcamCLOG
fi
fi
rm -f $CHECKFILE
fi
}
case "$1" in
start)
echo "[SCRIPT] $1:"
start_cam
;;
stop)
echo "[SCRIPT] $1:"
stop_cam
;;
restart)
echo "[SCRIPT] $1:"
stop_cam
sleep 2
start_cam
;;
check)
echo "[SCRIPT] $1:"
check_run
;;
*)
echo "Usage: $0 [start|stop|restart|check]"
exit 1
;;
esac
exit 0