一 问题
自己开发的程序,如何用shell 脚本实现 启动,关闭,重启,查看状态?
二 方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    #!/bin/sh
    SERVICE="my_service"
    start(){
        echo "starting..."
        nohup $SERVICE > $SERVICE.log 2>&1 &  
        if [ $? -ne 0 ]
        then
            echo "start failed, please check the log!"
            exit $?
        else
            echo $! > $SERVICE.pid 
            echo "start success"
        fi
    }
    stop(){
        echo "stopping..."
        kill -9 `cat $SERVICE.pid`
        if [ $? -ne 0 ]
        then
            echo "stop failed, may be $SERVICE isn't running"
            exit $?
        else
            rm -rf $SERVICE.pid 
            echo "stop success"
        fi
    }
    restart(){
        stop&&start
    }
    status(){
        num=`ps -ef | grep $SERVICE | grep -v grep wc -l`
        if [ $num -eq 0 ]
        then
            echo "$SERVICE isn't running"
        else
            echo "$SERVICE is running"
        fi
    }
    case $1 in    
        start)      start ;;  
        stop)      stop ;;  
        restart)  restart ;;
        status)  status ;; 
        *)          echo "Usage: $0 {start|stop|restart|status}" ;;     
    esac  
  
    exit 0