#!/bin/bash
#-------------------------------------------------------------------------------
# OSCam Startscript for Gen2VDR based on Gentoo Linux
# Original written by Duke69 and -=Robby=- from
Sie müssen registriert sein, um Links zu sehen.
# 2010-05-27 v1.7
# Modified by MegaV0lt 2012-11-21
#-------------------------------------------------------------------------------
# Watchdog function which checks maximum/minimum oscam pids all xxx sec.
# The maximum of OSCam Pids is 32.
# When minimum of Oscam pids is below defined value, OSCam is restartet.
# In both cases oscamerror.log is created containing a printout of
# command ps and the last 200 lines of OSCam.log.
# The minimum of OSCam pids is set by variable "minpids" (default: 1).
# The seconds are set by variable "watchdogtime" (default: 10).
#-------------------------------------------------------------------------------
# Creates a oscamstatus.log logfile in /var/log/oscam which
# logs all relevant start/stop/restart/reboot messages.
# Logfilesize limited by variable "maxlogsize" (Bytes)
# if limit exceeds status-log is moved to oscamstatus-prev.log.
#-------------------------------------------------------------------------------
# setup:
#===============================================================================
MAXLOGSIZE=102400 # byte
MINPIDS=1
WATCHDOGTIME=10 # seconds
# paths and files
#===============================================================================
LOGDIR=/var/log/oscam
#OSCAMDIR=/usr/local/src/_div/oscam
OSCAMDIR=$(dirname $0) # Skript liegt im OSCam-Verzeichnis
STATUSLOG=$LOGDIR/oscam_status.log
STATUSPREVLOG=$LOGDIR/oscam_status-prev.log
OSCAMERRORLOG=$LOGDIR/oscam_error.log
OSCAMERRORPREVLOG=$LOGDIR/oscam_error-prev.log
#===============================================================================
log() {
echo "$(date +"%Y-%m-%d %H:%M.%S") > $1" >> $STATUSLOG
}
start_oscam() {
[ -e "$OSCAMDIR/checkoscam.sh" ] && $OSCAMDIR/checkoscam.sh
if [ -e "${OSCAMDIR}/.loglevel" ] ; then
LOGLEVEL="-d $(cat ${OSCAMDIR}/.loglevel)"
log "Starting oscam with DebugLog: ${LOGLEVEL}"
fi
# Use start-stop-daemaon to create pidfile
start-stop-daemon -S -m -p /var/run/oscam.pid -b \
-x $OSCAMDIR/oscam -- -c $OSCAMDIR ${LOGLEVEL}
sleep 8
}
err_reboot() {
if [ -e $OSCAMERRORLOG ] ; then
mv $OSCAMERRORLOG $OSCAMERRORPREVLOG
fi
uptime >> $OSCAMERRORLOG
echo "" >> $OSCAMERRORLOG
ps >> $OSCAMERRORLOG
echo "" >> $OSCAMERRORLOG
tail -200 >> $OSCAMERRORLOG
sleep 1
log "Ups... This should not happen."
#reboot
}
check_log() {
FILESIZE=$(stat -c %s $STATUSLOG)
if [ $FILESIZE -ge $MAXLOGSIZE ] ; then
log "max. logfile size ($MAXLOGSIZE bytes) exceeded, creating new logfile"
mv $STATUSLOG $STATUSPREVLOG
fi
}
start_oscam # start oscam
if pidof oscam > /dev/null ; then
log "OSCam started"
check_log # check logfile size
fi
while true ; do
OSCAMPIDS=$(pidof oscam | wc -w)
if [ $OSCAMPIDS -eq 32 ] ; then
log "maximum OSCam pids reached"
check_log # check logfile size
err_reboot # create errorlog and reboot
else if [ $OSCAMPIDS -lt $MINPIDS ] ; then
log "below minimum OSCam pids, restarting OSCam"
check_log # check logfile size
killall oscam
sleep 3
if pidof oscam > /dev/null ; then
log "OSCam stopped but waste pids alive"
check_log # check logfile size
err_reboot # create errorlog and reboot
fi
start_oscam # start oscam
if pidof oscam > /dev/null ; then
log "OSCam restarted"
fi
check_log # check logfile size
fi
fi
sleep $WATCHDOGTIME
done