Daemonizing Problem - Serious

Hello All,

I have a serious problem about daemonizing my script ( an executable, I named it as myScript at below ). I use standart template ( Centos 5.9 ) but due to a problem I could not figure out the reason, after random time it fails to start, and since I made it in while loop it restarts forever. For example, it works 2-3 hours, then it fails and begins to restart, or it fails at the first try and restarts forever...

And another thing is, I have a second script ( named it as mySecondScript at below ) and while I start them ( second script first, then the first script ) there is no problem, but while stopping them, first script fails to stop ( because of killing process or removing pid ), what should I change in process catching line, thanks in advance.

Here is the code;

#!/bin/sh
#
#chkconfig: 2345 99 30
#description: myScript
#processname: myScript

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 1
# Process name ( For display )
NAME=myScript
# Daemon name, where is the actual executable
DAEMON=/myScript/APP/myScript
# Daemon user
DAEMONUSER=scriptUser
# pid file for the daemon
PIDFILE=/var/run/myScript/myScript.pid
# log file for deamon
LOGFILE=/var/log/myScript.log

        # Define variables
        PS_CMD="ps -e -o pid,args"
        proc_list=`$PS_CMD | grep $NAME | grep -v grep | grep -v sh | grep -v /myScript/APP/mySecondScript | grep -v vi | grep -v su | grep -v tail`
        procs=`echo $proc_list | awk '{print $1}'`

start () {
        # see if it exists
        if [ -n "$procs" ]; then
                echo "$NAME is already running..."
        elif [ -e "$PIDFILE" ]; then
                echo "$NAME is already running..."
        else
                # start daemon
                touch $PIDFILE
                while [ 1 ]
                do
                        nohup $DAEMON >>$LOGFILE 2>&1 &
                        #daemon +9 --check $NAME $DAEMON >> $LOGFILE

                        echo "#--------------------------------------------------#" >> $LOGFILE
                        echo "# $NAME quit due to error, re-running in 15 seconds #" >> $LOGFILE
                        echo "#--------------------------------------------------#" >> $LOGFILE

                        sleep 15
                done
        fi
}

stop () {
        # see if it exists
        if [ -n "$procs" ]; then
                # stop daemon
                killproc $NAME
                rm -rf $PIDFILE
                echo -e "\n`date +%a' '%b' '%d' '%H:%M:%S' '%Y` - $NAME has been stopped...\n" >> $LOGFILE
        elif [ -e "$PIDFILE" ]; then
                rm -rf $PIDFILE
                echo -e "\n`date +%a' '%b' '%d' '%H:%M:%S' '%Y` - $NAME pid file deleted...\n" >> $LOGFILE
                killproc $NAME
        else
                killproc $NAME
                echo "$NAME is not running..."
        fi
}

case "$1" in
  start)
        start &
    ;;
  stop)
        stop
    ;;
  status)
        status $NAME
        RETVAL=$?
        ;;
  *)
    echo $"Usage: $0 {start|stop|status}"
    exit 1
    ;;
esac

exit 0

and here is the log file;

Thu May  9 12:03:17 2013 - myScript has been started...

^[[60G[^[[0;32m  OK  ^[[0;39m]^M#--------------------------------------------------#
# myScript quit due to error, re-running in 15 seconds #
#--------------------------------------------------#

Thu May  9 12:03:33 2013 - myScript has been started...

^[[60G[^[[0;32m  OK  ^[[0;39m]^M#--------------------------------------------------#
# myScript quit due to error, re-running in 15 seconds #
#--------------------------------------------------#

Thu May  9 12:03:48 2013 - myScript has been started...

^[[60G[^[[0;32m  OK  ^[[0;39m]^M#--------------------------------------------------#
# myScript quit due to error, re-running in 15 seconds #
#--------------------------------------------------#

Thu May  9 12:04:04 2013 - myScript has been started...

^[[60G[^[[0;32m  OK  ^[[0;39m]^M#--------------------------------------------------#
# myScript quit due to error, re-running in 15 seconds #
#--------------------------------------------------#

Thu May  9 12:04:19 2013 - myScript has been started...

^[[60G[^[[0;32m  OK  ^[[0;39m]^M#--------------------------------------------------#
# myScript quit due to error, re-running in 15 seconds #
#--------------------------------------------------#

Thu May  9 12:04:35 2013 - myScript has been started...

^[[60G[^[[0;32m  OK  ^[[0;39m]^M#--------------------------------------------------#
# myScript quit due to error, re-running in 15 seconds #
#--------------------------------------------------#

It is nice if a daemon has 1 as parent, by a killed parent shell. But a true daemon should not be in an interactive process group or related to a controlling terminal, so I often launched mine with at, crontab (auto restart as a bonus) or an ssh nohup that kills ssh after the nohup, or off an xtern that I then kill. Of course, it you have root you can have init.d or some daemon manager like src launch it for you on boot.

I used to lose my Xserver's mwm from ctrl-c SIGINT on the launching xterm; drove me nuts until I used the first xterm to launch everything and then killed it, making them all orphans, or devoted it to top.