#!/bin/bash   
#   singhead monitor for astercc daemons

#     
# |||||||||||||||||||| CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------   
#

# the absolute path to your monitor log, can not include logfile name
monitorlogpath="/opt/asterisk/scripts/astercc/"
i=0
#
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

#astccwatchdogpid=`ps -ef |grep -v grep |grep -E /astcc_watchdog |awk '{print $2}'`
#echo $astccwatchdogpid
#exit;
#if [ "x${astccwatchdogpid}" != "x" ]; then
#	astccwatchdogSTATUS="astcc_watchdog already running(pid $astccwatchdogpid)"
#	echo $astccwatchdogSTATUS
#	exit
#    astccwatchdogRUNNING=1
#else
#	astccwatchdogSTATUS="astcc_watchdog is not running"
#    astccwatchdogRUNNING=0
#fi

if [ ! -f $monitorlogpath/safe_astercc.log ];then 
echo 0 > $monitorlogpath/safe_astercc.log
else
sed -i '1d' $monitorlogpath/safe_astercc.log
sed -i '1i\0' $monitorlogpath/safe_astercc.log
fi

asterccconf="/etc/astercc.conf"
asterccc=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep asterccc[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
asterccctry=0
#astccstatus=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astccstatus[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astccrealtime=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astccrealtime[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astccrealtimetry=0
astccpush=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astccpush[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astccpushtry=0
astccdialer=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astccdialer[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astccdialertry=0
astccdatamover=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astccdatamover[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astccdatamovertry=0
mailman=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep mailman[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
mailmantry=0
readevents=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep readevents[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
readeventstry=0
astccautodialer=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astccautodialer[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astccautodialertry=0
astcc_statistics=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astccstatistics[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astccstatisticstry=0
astcc_sysevents=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astccsysevents[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astccsyseventstry=0
astcc_smsman=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astccsmsman[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astccsmsmantry=0
socketstruct=`sed -n '/\[zrcti]/,/\[.*\]/p' ${asterccconf} |grep zrcti_masterip[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
socketstructtry=0
astcclocalcheck=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep astcclocalcheck[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
astcclocalchecktry=0
socketevent=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep socketevent[\ ]*= |grep -v "^[\ *\;\ *]" |head -n 1 |cut -d= -f2 |tr -d " "`
socketeventtry=0

maxtry=5

sleeptime=`sed -n '/\[system]/,/\[.*\]/p' ${asterccconf} |grep wacthdog_sleep[\ ]*= |grep -v "^[\ *\;\ *]" |cut -d= -f2 |tr -d " "`

sleeptime=`echo $sleeptime |grep -e '\<[1-9][0-9]\+\>'`

if [ "X$sleeptime" == "X" ]
then
  sleeptime=30
fi

asteriskserver=`sed -n '/\[asterisk]/,/\[.*\]/p' ${asterccconf} |grep server[\ ]*= |grep -v "^[\ *\;\ *]" |cut -d= -f2 |tr -d " "`

run_astercc (){
while :; do
#for fax
if [ -f /opt/asterisk/scripts/astercc/initfax.sh ];then
cat /opt/asterisk/scripts/astercc/initfax.sh | while read -r faxline
do
prefix=`echo ${faxline}|cut -d\/ -f2`
if [ "${prefix}" == "usr" ];then
cmd=`echo ${faxline}|cut -d' ' -f1`
faxdevice=`echo ${faxline}|cut -d' ' -f2`
#echo "'${cmd} ${faxdevice}'"
ps -ef |grep -v grep |grep -c "${cmd} ${faxdevice}"  &> /dev/null
#echo "grep:$?"
if [ $? = 1 ]
then
echo `date`" staring ${faxdevice}" >> $monitorlogpath/safe_astercc.log
${cmd} ${faxdevice} &
fi
fi
done
fi

#for mailman
if [ "x${mailman}" != 'x' -a $mailmantry -lt $maxtry ]; then

if test -s "/var/run/astcc_mailman.pid"
then
mailmanpid=`cat /var/run/astcc_mailman.pid`
else
mailmanpid='';
fi
testpid=`pidof astcc_mailman |awk '{print $1}'`
if [ "x$testpid" == "x" ]; then
mailmanpid='';
rm -rf /var/run/astcc_mailman.pid
$mailman
if [ $? = 0 ]
then
mailmantry=0
else
let "mailmantry=$mailmantry + 1"
fi
fi	
fi
#for astccrealtime
if [ "x${astccrealtime}" != 'x' -a $astccrealtimetry -lt $maxtry ]; then

if test -s "/var/run/astcc_realtime-${asteriskserver}.pid"
then
astccrealtimepid=`cat /var/run/astcc_realtime-${asteriskserver}.pid`
else
astccrealtimepid='';
fi
testpid=`pidof astcc_realtime |awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$astccrealtimepid" != "$testpid" ]; then
echo $testpid > /var/run/astcc_realtime-${asteriskserver}.pid
astccrealtimepid=$testpid
fi
else
astccrealtimepid='';
rm -rf /var/run/astcc_realtime-${asteriskserver}.pid
fi


if [ "x${astccrealtimepid}" == "x" ]; then
if test -s $astccrealtime
then
$astccrealtime -d	

if test -s "/var/run/astcc_realtime-${asteriskserver}.pid"
then
astccrealtimepid=`cat /var/run/astcc_realtime-${asteriskserver}.pid`
else
astccrealtimepid='';
fi
if [ "x$astccrealtimepid" != "x" ] ; then
echo `date`" $0 $ARG: astcc_realtime started" >> $monitorlogpath/safe_astercc.log
astccrealtimetry=0
else	
echo `date`" $0 $ARG: astcc_realtime start failed" >> $monitorlogpath/safe_astercc.log
let "astccrealtimetry=$astccrealtimetry + 1"
fi
else	
echo `date`" $0 $ARG: can not find astcc_realtime in"$astccrealtime >> $monitorlogpath/safe_astercc.log
let "astccrealtimetry=$astccrealtimetry + 1"
fi
fi
fi
#for readevents
if [ "x${readevents}" != 'x' -a $readeventstry -lt $maxtry ]; then

if test -s "/var/run/readevents.pid"
then
readeventspid=`cat /var/run/readevents.pid`
else
readeventspid='';
fi
testpid=`pidof readevents |awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$readeventspid" != "$testpid" ]; then
echo $testpid > /var/run/readevents.pid
readeventspid=$testpid
fi
else
readeventspid='';
rm -rf /var/run/readevents.pid
fi
if [ "x${readeventspid}" == "x" ]; then
if test -s $readevents
then
$readevents -d
if test -s "/var/run/readevents.pid"
then
readeventspid=`cat /var/run/readevents.pid`
else
readeventspid='';
fi
if [ "x$readeventspid" != "x" ] ; then
echo `date`" $0 $ARG: readevents started" >> $monitorlogpath/safe_astercc.log
readeventstry=0
else	
echo `date`" $0 $ARG: readevents start failed" >> $monitorlogpath/safe_astercc.log
let "readeventstry=$readeventstry + 1"
fi
else	
echo `date`" $0 $ARG: can not find readevents in"$readevents >> $monitorlogpath/safe_astercc.log
let "readeventstry=$readeventstry + 1"
fi
fi	
fi

#for astccautodialer
if [ "x${astccautodialer}" != 'x' -a $astccautodialertry -lt $maxtry ]; then

if test -s "/var/run/astcc_autodialer.pid"
then
astccautodialerpid=`cat /var/run/astcc_autodialer.pid`
else
astccautodialerpid='';
fi
testpid=`pidof astcc_autodialer |awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$astccautodialerpid" != "$testpid" ]; then
echo $testpid > /var/run/astcc_autodialer.pid
astccautodialerpid=$testpid
fi
else
astccautodialerpid='';
rm -rf /var/run/astcc_autodialer.pid
fi

if [ "x${astccautodialerpid}" == "x" ]; then
if test -s $astccautodialerpid
then
$astccautodialer -d
if test -s "/var/run/astcc_autodialer.pid"
then
astccautodialerpid=`cat /var/run/astcc_autodialer.pid`
else
astccautodialerpid='';
fi
if [ "x$astccautodialerpid" != "x" ] ; then
echo `date`" $0 $ARG: astccautodialer started" >> $monitorlogpath/safe_astercc.log
astccautodialertry=0
else	
echo `date`" $0 $ARG: astccautodialer start failed" >> $monitorlogpath/safe_astercc.log
let "astccautodialertry=$astccautodialertry + 1"
fi
else	
echo `date`" $0 $ARG: can not find astccautodialer in"$astccautodialer >> $monitorlogpath/safe_astercc.log
let "astccautodialertry=$astccautodialertry + 1"
fi
fi	
fi

#for astcc_statistics
if [ "x${astcc_statistics}" != 'x' -a $astccstatisticstry -lt $maxtry ]; then

if test -s "/var/run/astcc_statistics.pid"
then
astccstatisticspid=`cat /var/run/astcc_statistics.pid`
else
astccstatisticspid='';
fi
testpid=`pidof astcc_statistics |awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$astccstatisticspid" != "$testpid" ]; then
echo $testpid > /var/run/astcc_statistics.pid
astccstatisticspid=$testpid
fi
else
astccstatisticspid='';
rm -rf /var/run/astcc_statistics.pid
fi

if [ "x${astccstatisticspid}" == "x" ]; then
if test -s $astccstatisticspid
then
$astcc_statistics -d
if test -s "/var/run/astcc_statistics.pid"
then
astccstatisticspid=`cat /var/run/astcc_statistics.pid`
else
astccstatisticspid='';
fi
if [ "x$astccstatisticspid" != "x" ] ; then
echo `date`" $0 $ARG: astcc_statistics started" >> $monitorlogpath/safe_astercc.log
astccstatisticstry=0
else	
echo `date`" $0 $ARG: astcc_statistics start failed" >> $monitorlogpath/safe_astercc.log
let "astccstatisticstry=$astccstatisticstry + 1"
fi
else	
echo `date`" $0 $ARG: can not find astcc_statistics in"$astcc_statistics >> $monitorlogpath/safe_astercc.log
let "astccautodialertry=$astccstatisticstry + 1"
fi
fi	
fi

#for astcc_sysevents
if [ "x${astcc_sysevents}" != 'x' -a $astccsyseventstry -lt $maxtry ]; then

if test -s "/var/run/astcc_sysevents.pid"
then
astcc_syseventspid=`cat /var/run/astcc_sysevents.pid`
else
astcc_syseventspid='';
fi
testpid=`pidof astcc_sysevents |awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$astcc_syseventspid" != "$testpid" ]; then
echo $testpid > /var/run/astcc_sysevents.pid
astcc_syseventspid=$testpid
fi
else
astcc_syseventspid='';
rm -rf /var/run/astcc_sysevents.pid
fi

if [ "x${astcc_syseventspid}" == "x" ]; then
if test -s $astcc_syseventspid
then
$astcc_sysevents -d
if test -s "/var/run/astcc_sysevents.pid"
then
astcc_syseventspid=`cat /var/run/astcc_sysevents.pid`
else
astcc_syseventspid='';
fi
if [ "x$astcc_syseventspid" != "x" ] ; then
echo `date`" $0 $ARG: astcc_sysevents started" >> $monitorlogpath/safe_astercc.log
astccsyseventstry=0
else	
echo `date`" $0 $ARG: astcc_sysevents start failed" >> $monitorlogpath/safe_astercc.log
let "astccsyseventstry=$astccsyseventstry + 1"
fi
else	
echo `date`" $0 $ARG: can not find astcc_sysevents in"$astcc_sysevents >> $monitorlogpath/safe_astercc.log
let "astccsyseventstry=$astccsyseventstry + 1"
fi
fi	
fi

#for astccdatamover
if [ "x${astccdatamover}" != 'x' -a $astccdatamovertry -lt $maxtry ]; then

if test -s "/var/run/astcc_datamover.pid"
then
astccdatamoverpid=`cat /var/run/astcc_datamover.pid`
else
astccdatamoverpid='';
fi
testpid=`pidof astcc_datamover |awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$astccdatamoverpid" != "$testpid" ]; then
echo $testpid > /var/run/astcc_datamover.pid
astccdatamoverpid=$testpid
fi
else
astccdatamoverpid='';
rm -rf /var/run/astcc_datamover.pid
fi

if [ "x${astccdatamoverpid}" == "x" ]; then
if test -s $astccdatamover
then
$astccdatamover -d
if test -s "/var/run/astcc_datamover.pid"
then
astccdatamoverpid=`cat /var/run/astcc_datamover.pid`
else
astccdatamoverpid='';
fi
if [ "x$astccdatamoverpid" != "x" ] ; then
echo `date`" $0 $ARG: astcc_datamover started" >> $monitorlogpath/safe_astercc.log
astccdatamovertry=0
else	
echo `date`" $0 $ARG: astcc_datamover start failed" >> $monitorlogpath/safe_astercc.log
let "astccdatamovertry=$astccdatamovertry + 1"
fi
else	
echo `date`" $0 $ARG: can not find astcc_datamover in"$astccdatamover >> $monitorlogpath/safe_astercc.log
let "astccdatamovertry=$astccdatamovertry + 1"
fi
fi
fi

#for astccdialer
if [ "x${astccdialer}" != 'x' -a $astccdialertry -lt $maxtry ]; then
monitorparm=${monitorparm}' yes'

if test -s "/var/run/astcc_dialer-${asteriskserver}.pid"
then
astccdialerpid=`cat /var/run/astcc_dialer-${asteriskserver}.pid`
else
astccdialerpid='';
fi
testpid=`pidof astcc_dialer|awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$astccdialerpid" != "$testpid" ]; then
echo $testpid > /var/run/astcc_dialer-${asteriskserver}.pid
astccdialerpid=$testpid
fi
else
astccdialerpid='';
rm -rf /var/run/astcc_dialer-${asteriskserver}.pid
fi
if [ "x${astccdialerpid}" == "x" ]; then
if test -s $astccdialer
then
$astccdialer -d
if test -s "/var/run/astcc_dialer-${asteriskserver}.pid"
then
astccdialerpid=`cat /var/run/astcc_dialer-${asteriskserver}.pid`
else
astccdialerpid='';
fi
if [ "x$astccdatamoverpid" != "x" ] ; then
echo `date`" $0 $ARG: astcc_dialer started" >> $monitorlogpath/safe_astercc.log
astccdialertry=0
else	
echo `date`" $0 $ARG: astcc_dialer start failed" >> $monitorlogpath/safe_astercc.log
let "astccdialertry=$astccdialertry + 1"
fi
else	
echo `date`" $0 $ARG: can not find astcc_dialer in"$astccdialer >> $monitorlogpath/safe_astercc.log
let "astccdialertry=$astccdialertry + 1"
fi
fi	
fi

#for astcc_smsman
if [ "x${astcc_smsman}" != 'x' -a $astccsmsmantry -lt $maxtry ]; then

if test -s "/var/run/astcc_smsman.pid"
then
astcc_smsmanpid=`cat /var/run/astcc_smsman.pid`
else
astcc_smsmanpid='';
fi
testpid=`pidof astcc_smsman |awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$astcc_smsmanpid" != "$testpid" ]; then
echo $testpid > /var/run/astcc_smsman.pid
astcc_smsmanpid=$testpid
fi
else
astcc_smsmanpid='';
rm -rf /var/run/astcc_smsman.pid
fi

if [ "x${astcc_smsmanpid}" == "x" ]; then
if test -s $astcc_smsmanpid
then
$astcc_smsman -d
if test -s "/var/run/astcc_smsman.pid"
then
astcc_smsmanpid=`cat /var/run/astcc_smsman.pid`
else
astcc_smsmanpid='';
fi
if [ "x$astcc_smsmanpid" != "x" ] ; then
echo `date`" $0 $ARG: astcc_smsman started" >> $monitorlogpath/safe_astercc.log
astccsmsmantry=0
else	
echo `date`" $0 $ARG: astcc_smsman start failed" >> $monitorlogpath/safe_astercc.log
let "astccsmsmantry=$astccsmsmantry + 1"
fi
else	
echo `date`" $0 $ARG: can not find astcc_smsman in"$astcc_smsman >> $monitorlogpath/safe_astercc.log
let "astccsmsmantry=$astccsmsmantry + 1"
fi
fi	
fi
#for socketstruct
if [ "x${socketstruct}" != 'x' -a $socketstructtry -lt $maxtry ]; then

if test -s "/var/run/socket_struct.pid"
then
socketstructpid=`cat /var/run/socket_struct.pid`
else
socketstructpid='';
fi
testpid=`ps -ef |grep -v grep |grep -E '/socket_struct$|/socket_struct' |awk '{print $2}'`
if [ "x$testpid" != "x" ]; then
if [ "$socketstructpid" != "$testpid" ]; then
echo $testpid > /var/run/socket_struct.pid
socketstructpid=$testpid
fi
else
socketstructpid='';
rm -rf /var/run/socket_struct.pid
fi

if [ "x${socketstructpid}" == "x" ]; then
if test -s $socketstructpid
then
/opt/asterisk/scripts/astercc/socket_struct -d
if test -s "/var/run/socket_struct.pid"
then
socketstructpid=`cat /var/run/socket_struct.pid`
else
socketstructpid='';
fi
if [ "x$socketstructpid" != "x" ] ; then
echo `date`" $0 $ARG: socket_struct started" >> $monitorlogpath/safe_astercc.log
socketstructtry=0
else	
echo `date`" $0 $ARG: socket_struct start failed" >> $monitorlogpath/safe_astercc.log
let "socketstructtry=$socketstructtry + 1"
fi
else	
echo `date`" $0 $ARG: can not find socket_struct in"$socketstruct >> $monitorlogpath/safe_astercc.log
let "socketstructtry=$socketstructtry + 1"
fi
fi	
fi

#for astcclocalcheck
if [ "x${astcclocalcheck}" != 'x' -a $astcclocalchecktry -lt $maxtry ]; then

if test -s "/var/run/astcc_localcheck.pid"
then
astcclocalcheckpid=`cat /var/run/astcc_localcheck.pid`
else
astcclocalcheckpid='';
fi
testpid=`pidof astcc_localcheck |awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$astcclocalcheckpid" != "$testpid" ]; then
echo $testpid > /var/run/astcc_localcheck.pid
astcclocalcheckpid=$testpid
fi
else
astcclocalcheckpid='';
rm -rf /var/run/astcc_localcheck.pid
fi

if [ "x${astcclocalcheckpid}" == "x" ]; then
if test -s $astcclocalcheckpid
then
$socketstruct -d
if test -s "/var/run/astcc_localcheck.pid"
then
astcclocalcheckpid=`cat /var/run/astcc_localcheck.pid`
else
astcclocalcheckpid='';
fi
if [ "x$astcclocalcheckpid" != "x" ] ; then
echo `date`" $0 $ARG: astcc_localcheck started" >> $monitorlogpath/safe_astercc.log
astcclocalchecktry=0
else	
echo `date`" $0 $ARG: astcc_localcheck start failed" >> $monitorlogpath/safe_astercc.log
let "astcclocalchecktry=$astcclocalchecktry + 1"
fi
else	
echo `date`" $0 $ARG: can not find astcc_localcheck in"$astcclocalcheck >> $monitorlogpath/safe_astercc.log
let "astcclocalchecktry=$astcclocalchecktry + 1"
fi
fi	
fi

#for socketevent
if [ "x${socketevent}" != 'x' -a $socketeventtry -lt $maxtry ]; then

if test -s "/var/run/socketevent.pid"
then
socketeventpid=`cat /var/run/socketevent.pid`
else
socketeventpid='';
fi
testpid=`pidof socketevent |awk '{print $1}'`
if [ "x$testpid" != "x" ]; then
if [ "$socketeventpid" != "$testpid" ]; then
echo $testpid > /var/run/socketevent.pid
socketeventpid=$testpid
fi
else
socketeventpid='';
rm -rf /var/run/socketevent.pid
fi

if [ "x${socketeventpid}" == "x" ]; then
if test -s $socketeventpid
then
$socketevent -d
if test -s "/var/run/socketevent.pid"
then
socketeventpid=`cat /var/run/socketevent.pid`
else
socketeventpid='';
fi
if [ "x$socketeventpid" != "x" ] ; then
echo `date`" $0 $ARG: socketevent started" >> $monitorlogpath/safe_astercc.log
socketeventtry=0
else	
echo `date`" $0 $ARG: socketevent start failed" >> $monitorlogpath/safe_astercc.log
let "socketeventtry=$socketeventtry + 1"
fi
else	
echo `date`" $0 $ARG: can not find socketevent in"$socketevent >> $monitorlogpath/safe_astercc.log
let "socketeventtry=$socketeventtry + 1"
fi
fi	
fi

#if [ "x${asterccc}" == 'x' -a $asterccctry -lt $maxtry ]; then
#asterccc="/opt/asterisk/scripts/astercc/asterccc"
#fi

if [ "x${asterccc}" != 'x' -a $asterccctry -lt $maxtry ]; then
monitorparm=${monitorparm}' yes'

if test -s "/tmp/asterccc.pid"
then
astercccpid=`cat /tmp/asterccc.pid`
else
astercccpid='';
fi
testpid=`ps -ef |grep -v grep |grep -E '/asterccc$|/asterccc' |awk '{print $2}'`

if [ "x$testpid" == "x" ]; then	
astercccpid='';
rm -rf /tmp/asterccc.pid
fi
if [ "x${astercccpid}" == "x" ]; then
if test -s $asterccc
then
$asterccc -d
sleep 30
if test -s "/tmp/asterccc.pid"
then
astercccpid=`cat /tmp/asterccc.pid`
else
astercccpid='';
fi
if [ "x$astercccpid" != "x" ] ; then
echo `date`" $0 $ARG: asterccc started" >> $monitorlogpath/safe_astercc.log
asterccctry=0
else	
echo `date`" $0 $ARG: asterccc start failed" >> $monitorlogpath/safe_astercc.log
let "asterccctry=$asterccctry + 1"
fi
else	
echo `date`" $0 $ARG: can not find asterccc in"$asterccc >> $monitorlogpath/safe_astercc.log
let "asterccctry=$asterccctry + 1"
fi
fi
fi
sleep  ${sleeptime}
done
}

run_astercc &