shell脚本启动程序,防止重复启动

来源:互联网 发布:易知投资待遇 编辑:程序博客网 时间:2024/06/05 15:54


#!/bin/sh

programdir="."
program="test.py"

export CLASSPATH=$programdir
export LANG=zh_CN

startup()
{
        res=`ps aux|grep python|grep $program|grep -v grep|awk '{print $2}'`
        if [ -n "$res" ]
            then
                echo "$program already running"
        else
             nohup python $program > startup_out.file 2>&1  &
             sleep 3
             unset res
             res=`ps aux|grep python|grep $program|grep -v grep|awk '{print $2}'`
             if [ -n "$res" ]
                 then
                    echo " $program start success"
             else
                    echo "$program  start error"
             fi
        fi
}

shutdown()
{
        proc_id=`ps aux|grep python|grep $program|grep -v grep|awk '{print $2}'`
        if [ -n "$proc_id" ]
            then
                echo " $program    is    start,now kill......"
                kill -9    $proc_id
                echo " $proc    kill        ok !!!!!!!!!!!!!"    
        else
                echo " $program    is    not    start!!!!!!!!!!!"
        fi
}        


case "$1" in
        start)
                startup
                ;;
        stop)
                shutdown
                ;;
        restart)
                shutdown
                startup
                ;;

        *)
                echo "Usage: {start|stop|restart}" >&2
                exit 1
                ;;
esac
exit

0 0