Question: supervise is one of the powerful tool in daemontools, I wonder how to implement it using bash script. Anyone has suggestions? I need help!

Answer: 
`supervise` performs a number of tasks, and interoperates with `svscan`, `svcok`, `svstat`.
A fully-featured implementation in bash would be non-trivial, but a daemon-restarting script is a fairly straight-forward task.

```
#!/bin/bash
DAEMON=/usr/sbin/whatever      # "/bin/sleep" for demo
DAEMON_ARGS="xxx"              # "15" for sleep demo

case "$1" in 
    start) 
        echo "Starting $DAEMON"
        (
            trap 'logger -i -p daemon.info "INFO: $DAEMON shutting down..."; exit 1' 1 2 3 15
            logger -i -p daemon.info "INFO: Starting $DAEMON"
            while : ; do
                $DAEMON $DAEMON_ARGS &
                pid=$!
                echo $pid > /var/run/mydaemon.pid
                wait $pid
                rc=$?
                logger -i -p daemon.warn "WARNING: $DAEMON exited, rc=$rc"
                sleep 1  # adjust as required
                logger -i -p daemon.warn "WARNING: Restarting $DAEMON"
            done
        ) &
        echo $! > /var/run/myscript.pid
    ;;        

    stop)
        echo "Stopping $DAEMON"
        [ -f /var/run/myscript.pid ] && kill $(</var/run/myscript.pid)
        [ -f /var/run/mydaemon.pid ] && kill $(</var/run/mydaemon.pid)
        ;;
esac

```

The above has pretty much no error handling, doesn't properly do real daemon things like chdir() and close unused FDs, but it does log via logger/syslog so you can see what it's doing. It assumes `$DAEMON` does not fork into the background itself (as does `supervise`).

You haven't stated your platform, but if you want something really, really simple, inittab may do the trick, see [how to use inittab to auto-restart a PHP programme?](https://stackoverflow.com/questions/937160/how-to-use-inittab-to-auto-restart-a-php-programme) for some tips. Otherwise we're veering out of Stack Overflow territory, so check out <https://unix.stackexchange.com/> .
I changed [the code provided by mr.spuratic](https://stackoverflow.com/a/14345093) around a little bit:

```
echo "true" > $STATUSFILE

case "$1" in

    start) 
        echo "Starting $DAEMON_NAME"
        (
            trap 'logger -t italoService "INFO: $DAEMON_NAME shutting down..."; exit 1' 1 2 3 15
            logger -t italoService "INFO: Starting $DAEMON_NAME"
            while : ; do
                #do_start
                read STATUS <$STATUSFILE
                if [ "$STATUS" = "false" ] ; then
                     logger -t italoService "INFO: $DAEMON_NAME stopped by user"
                     break
                fi
                $DAEMON $DAEMON_OPTS &
                pid=$!
                echo $pid > $PIDFILE
                logger -t italoService "WARNING: pid File: $pid"
                wait $pid
                rc=$?
                logger -t italoService "WARNING: $DAEMON_NAME exited, rc=$rc"
                sleep 1  # adjust as required
                read STATUS <$STATUSFILE
                if [ "$STATUS" = "true" ] ; then
                     logger -t italoService "WARNING: Restarting $DAEMON_NAME"
                fi
            done
        ) &
        echo $! > $PIDFILE
    ;;        

    stop)
        echo "Stopping $DAEMON_NAME"
        echo "false" > $STATUSFILE
        [ -f $PIDFILE ] && kill $(<$PIDFILE)
        [ -f $PIDFILE ] && kill $(<$PIDFILE)
        ;;

```