openWRT BASH 脚本查询进程是否运行,以及未运行时重启

系统: openWRT v15, CHAOS CALMER

脚本: BASH


步骤:

1. 更新数据源及安装 BASH;

#opkg update
#opkg install bash

2. 脚本文件, check_process.sh

#!/bin/bash

while [ 1 ]
do
        flag=$(ps | grep check_process | grep -v "grep" | wc -l)
        if [ $flag = "1" ]
        then
                echo "check_process RUNNING!"
        else
                echo "check_process has stopped."
                #check_process start up
        fi

        sleep 5
done

 注意事项: 进程名 check_process 的唯一性,否则 if 比较 "1" 不正确

4. 把脚本设为开机启动;

4.1 新建启动文件:
    #vi /etc/init.d/usart_1_run_monitor
 
4.2 文件内容:
#!/bin/sh /etc/rc.common
#/etc/init.d/usart_1_run_monitor
START=82
start() {
    #指定目录下的 bash 脚本,check_process.sh
}
 
stop(){
    killall usart_1_run_monitor
}
 
4.3 修改文件属性:
    #chmod 777 /etc/init.d/usart_1_run_monitor
 
4.4 启用开机启动服务
    #/etc/init.d/usart_1_run_monitor enable

猜你喜欢

转载自blog.csdn.net/weixin_42396877/article/details/82836552