使用shell编写的极简WatchDog

WatchDog-看门狗程序用于自动监测进程的运行状态,并按照须要重启进程。对于嵌入式系统的存储服务、网络服务程序常常由于各类错误出现中断,可使用WatchDog来自动保持服务的可用。WatchDog可使用shell来进行编写,这里给出一个极简的实现。shell

一、基本逻辑

  • 检查进程是否存在。
  • 若是进程不存在,则启动之。
  • 若是进程存在,则跳过,休眠给定时间。
  • 再次检查进程,重复上面的步骤。

二、极简版本

编写脚本,以下:ubuntu

#!/bin/bash
while true; do
    ret=`ps -aux | grep "22.*openthings" | grep -v grep | wc -l`
    if [ $ret -eq 0 ]; then
        nohup sshpass -p password ssh -y -NgL 2200:localhost:22 username@openthings.x.x &
    fi
    sleep 6
done

每6秒钟检查一次ssh命令的进程,若是退出了自动从新启动。安全

More simple:bash

#!/bin/bash
while true; do
    sshpass -p password ssh -y -NgL 2200:localhost:22 username@openthings.x.x &
    sleep 10
done

三、运行WatchDog

使用nohup和&让其在后台运行,也可使用screen软件或放在rc.local做为服务运行。网络

nohup ssht-watchdog &

在本地机器,检查是否在运行:ssh

ps -x

在远程机器,检查ssh的端口:测试

netstat -a | grep "220\|2088"

四、详细说明

4.1 提取进程信息

以下:this

ps -aux | grep "22.*192.168.1.1" | grep -v grep | awk -F " " '{print $1,$2}'

能够根据指定的信息(上面为 “22.*192.168.1.1”)查询进程信息列表,排除grep本身(grep -v grep),经过awk提取信息,按照空格分离字段,并输出前两个字段的值(进程ID/tty)。能够灵活运用来操做进程,如kill 指定进程等等。ps的参数在不一样操做系统有所不一样,请根据系统而定。spa

4.2 WatchDog脚本

以下:操作系统

#!/bin/bash

# 获取当前时间。
now=`date '+%Y-%m-%d %H:%M:%S'`

# 检测间隔的休眠时间。
sleepTime=6

# 检查进程的关键词,".*"为“且”关系,有顺序。
# 必定要包含在下面的命令中,不然或不断启动新的进程!
grepFlag='22.*openthings'

# 基本目录和日志文件名。
baseDir="/home/smt/openthings/tutools"
thisLog='/home/smt/openthings/tutools/tulog'

# 检查的主循环,无穷循环。
while true; do
    # 按照上面设定的关键词获取进程,若是存在返回1。
    ret=`ps aux | grep "$grepFlag" | grep -v grep | wc -l`

    # 若是不存在,则启动之。
    if [ $ret -eq 0 ]; then
        cd $baseDir
        echo "$now process not exists ,restart process now... " > "$thisLog"
        
        # 能够是任何shell命令。这里以ssh任务说明,不一样操做系统和版本的ssh命令参数不一样。
        # 密钥对须要预先生成,并上传公钥到服务端,追加到/etc/ssh/authrized_keys文件。
        # 这里是DropBear的用法。
        ssh -K 30 -y -fNgL 2200:localhost:22 username@openthings.x.x -i /opt/etc/.ssh/id_ecdsa >/dev/null 2>&1 &
        # 这里是openssh的用法。
        #ssh -y -fNgL 2200:localhost:22 username@openthings.x.x -i ~/.ssh/id_ecdsa >/dev/null 2>&1 &

        echo "$now restart done ..... "  > "$thisLog"
        cd $curDir
    else # 若是已存在,输出日志便可,不作任何其它操做。
        echo "$now process exists , sleep $sleepTime seconds " > "$thisLog"
    fi

    # 休眠一段时间,从新循环。
    sleep $sleepTime
done

其中的运行任务,上面使用免密钥登陆(参考:《在Asus Merlin固件的Dropbear使用及免密登陆》、《SSH免密直接登陆方法》),也可使用ssdpass来完成(参考:《Ubuntu上使用sshpass远程脚本免密安全交互》、《SSH如何保持链接-Ubuntu》),以下:

nohup sshpass -p mypassword ssh -y -fNgL 2200:localhost:22 username@openthings.x.x &
  • 注意:
    • 这里的ssh和sshpass的后台运行机制不一样,参考后面的资料。
    • 具体操做系统的实现有一些差别,须要进行仔细测试后部署。

参考资料

ssh远程运行命令,退出终端后继续执行,参考:

相关文章
相关标签/搜索