鹰击长空yuetao.org

4月 15

[原创]linux 服务性shell脚本代码范例 不指定

yuetao , 18:44 , server , 评论(0) , 引用(0) , 阅读(15037) , Via 本站原创 | |
----------------------------------------------------------------------
#!/bin/bash
#echo "$shijian start done." >> /home/yuetao/plan_task/wifi_mac_unlock.log
www_path="/home/yuetao/www/web"
www_name="web"
log_path="/home/yuetao/plan_task/wifi_mac_unlock.log"
mac_log="${www_path}/mac_lock.txt"
nohup_log_path="/home/yuetao/plan_task/wifi_mac_unlock.nohup"
pid_path="/home/yuetao/plan_task/wifi_mac_unlock.pid"


check_start () {
    pid=`get_pid`
    status=`check_status`
    if [ "$status" = "1" ]
    then
        echo "$www_name mac unlock allready run pid:$pid."
        ps aux | grep $pid
        exit 1
    fi
}

check_stop () {
     pid=`get_pid`
         status=`check_status`
         if [ "$status" = "0" ]
         then
                 echo "$www_name mac unlock count not run pid:$pid."
         ps aux | grep $pid
                 exit 1
         fi

}
check_status (){
    pid=`get_pid`
        psnum=`ps aux | grep "$pid" | wc -l`
        if [ "$psnum" = "1" ]
        then
        return 0             
    else
        return 1
        fi
}
get_pid () {
    cat "$pid_path"
}

save_pid () {
    pid=$1
    echo $! > $pid_path
}

save_log () {
    content="$1"
    echo "$content"
    echo "$content" >> $log_path
}

case "$1" in
 start)
     check_start
         cd $www_path
     nohup php home_task.php PlanTask mac_unlock > $nohup_log_path&
     save_pid $!
     save_log "$www_name mac unlock started pid:$pid."
         ;;
 stop)
     check_stop
         pid=`get_pid`
     kill $pid
     save_log "$www_name mac unlock stop success pid:$pid."
         ;;
 log)
    less $mac_log
    ;;
 cleanlog)
    echo "" > $mac_log
    echo "clearn log done."
    ;;
 status)
    pid=`get_pid`
        status=`check_status`
        if [ "$status" = "0" ]
        then
            echo "$www_name mac unlock not run."
        ps aux | grep $pid
            exit 1
    else
            echo "$www_name mac unlock run normal pid:$pid."
        ps aux | grep $pid
            exit 1
        fi
    ;;
 pid)
    pid=`get_pid`
    echo $pid
    ;;
 --help)
    echo "now not have help content."
    ;;
 *)
       echo "Usage: $0 {start | stop | log | cleanlog | status | pid}"
       exit 1
 esac
 exit 1
------------------------------------------------------------------
以上内容保存成文件
运行 空格 跟着 参数
Tags:
发表评论

昵称

主页

邮箱

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]