shell的通用启动与管理脚本(基于nohup)

shell脚本 (文件名:my_exec.sh)

#############  配置项 ###############################

keyword='a.php'                                      #查找进程是否存在的关键词
line=`ps aux | grep ${keyword} | wc -l`     # 查看进程是否存在的命令 
commend='php a.php'                   # nohup  执行的commend; 不要带 输出重定向 和 & !!!!!!
output_file='1.txt'                                      #输出重定向文件
pid_file='exec_pid.pid'                              #pid文件 确保唯一且文件夹可写

############  配置项 end ############################

###检查状态
function check_running(){
        if [[ $line -lt 2 ]]     ## 去除 gerp 时产生的进程
        then
                echo "未运行"    #####  未执行的操作
        else
                echo "正在运行"  #####  已执行的操作
        fi
}

function start_running(){

        if [[ $line -lt 2 ]]
        then
                echo "正在开始。。。。"
                eval "nohup ${commend} >>${output_file} 2>&1 &"
                echo $! > ${pid_file}                 #### 保存pid 文件
                sleep 2
                echo "成功"
        else
                echo "程序已经运行,请不要重复运行"

        fi
}

function stop_running(){
        if [[ $line -lt 2 ]]
        then
                echo "程序未运行"
        elif [ ! -f "$pid_file" ]
        then
                echo "pid文件不存在,请使用 kill 删除进程"
        else
                echo "正在停止。。。。"
                kill -9 `cat ${pid_file}`  #####  自己的停止方法 尽量不要用 kill -9
                rm -f  $pid_file
                sleep 2
                echo "成功"
        fi

}
case $1 in
        start):
                start_running;
                ;;
        check):
                check_running;
                ;;
        stop):
                stop_running;
                ;;
        restart):
                stop_running;
                start_running;
                ;;
        *):
                echo "请输入命令: sh start_exec.sh【start|stop|restart|check】"

                 ;;

esac
 

php脚本  (自己的后台脚本,一定要是能够长久运行的)     

<?php
        while(true){
                sleep(1);
                file_put_contents('1.txt',date('Y-m-d H:i:s')."\r\n",FILE_APPEND);
        }

?>
 

方法  sh shart_exec.sh  (start | stop | restart | check)

 

转载于:https://my.oschina.net/u/2528821/blog/1553773

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值