1. 程式人生 > 其它 >啟動指令碼和守護程序

啟動指令碼和守護程序

技術標籤:BashLinux啟動指令碼

#!/bin/bash
# load_dypserver.sh
name='dypserver'
SV="./supervise.$name"
unalias pidof &>/dev/null
exec 0</dev/null
#ulimit -c unlimited
cd `dirname $0`

#LIBRDKAFKA_DIR=`pwd`/lib
#export LD_LIBRARY_PATH=$LIBRDKAFKA_DIR:$LD_LIBRARY_PATH

if [ ! -d "log" ]; then
        mkdir -p "log"
fi
chmod 755 log

flush_bns() {
        date >> log/boot.log
        for bns in `cat conf/*.dsn |cut -d @ -f 2|cut -d : -f 1`
        do
                echo "get_instance_by_service -a "$bns >> log/boot.log
                get_instance_by_service -a $bns >> log/boot.log
        done
        sleep 1
}

start() {
        stop
       # flush_bns
        nohup $SV bin/$name &>/dev/null &
}
stop() {
        if [ -e $SV.pid ]; then
                cat $SV.pid | xargs /usr/bin/kill
        fi
        if [[ -e /usr/bin/ps ]]; then
                pid=`/usr/bin/ps --noheaders -o pid -C $name`
        else
                pid=`ps --noheaders -o pid -C $name`
        fi
        if [ "$pid" != "" ]; then
                /usr/bin/kill $pid
        fi
        if [ -e $SV.pid ]; then
                rm $SV.pid
        fi
}
reload() {
        if [[ -e /usr/bin/ps ]]; then
                pid=`/usr/bin/ps --noheaders -o pid -C $name`
        else
                pid=`ps --noheaders -o pid -C $name`
        fi
        if [ "$pid" != "" ]; then
                /usr/bin/kill -SIGHUP $pid
        else
                start
        fi
}
case C"$1" in
        C)
                reload
                echo "Done!"
                ;;
        Cstart)
                start
                echo "Done!"
                ;;
        Cstop)
                stop
                echo "Done!"
                ;;
        Creload)
                reload
                echo "Done!"
                ;;
        C*)
                echo "Usage: $0 {start|stop|reload}"
                ;;
esac
#!/bin/bash
#!/bin/bash
# supervise.dypserver
pid=$$
echo -n "$pid" > $0.pid
app_name=$1
while [ 1 ]; do
        app_base_name=`basename $app_name`
        if [[ -e /usr/bin/ps ]]; then
                child_pid=`/usr/bin/ps  --noheaders -o pid -C $app_base_name`
        else
                child_pid=`ps  --noheaders -o pid -C $app_base_name`
        fi
        if [ "$child_pid" == "" ]; then
                /usr/bin/nohup $app_name &>/dev/null &
                wait $child_pid
        fi
        sleep 1
done