lean openwrt 里的 qbittorrent-nox 服务 如何监控其是否在运行并在发现服务挂掉时重启?

2021-01-10 11:48:52 +08:00
 AllenHua
  1. openwrt 没有 systemd
  2. 编写了一个 shell 脚本 配合 crond 服务一直没有成功
#!/bin/sh
while true
do
COUNT=$(ps |grep qbittorrent |grep -v grep |wc -l)
echo "current running qbittorrent process count: "$COUNT
if [ $COUNT -eq 0 ]
then

su - transmission <<EOF
/usr/bin/qbittorrent-nox --profile=/mnt/ThreeTB2/qbit_backup
echo "qbittorrent-nox process has been restarted!"
EOF

else
echo "qbittorrent-nox already started!"
fi
sleep 10
done
kill $!

以上文件放在 /mnt/ThreeTB2/qbittorrent.sh 已经添加 可执行权限

cron 脚本是 */3 * * * * /bin/ash /mnt/ThreeTB2/qbittorrent.sh > /tmp/crontab-qbittorrent-sh.log

测试的时候 用的 每隔三分钟 执行一次

但是调试了一段时间 发现并不靠谱…… 希望大神前来指点

427 次点击
所在节点    OpenWrt
1 条回复
AllenHua
2021-01-10 12:16:25 +08:00
重新编写了一个 `/mnt/ThreeTB2/check_qbittorrent.sh`

```bash
#!/bin/bash
# author: xxx
# time: 2021-01-10
# program: check if qbittorrent-nox is running, if not, start it

function check_qbittorrent(){
qbittorrentCount=`ps |grep qbittorrent-nox |grep -v grep |wc -l`
echo "current running qbittorrent process count: "$qbittorrentCount
if [ 0 == $qbittorrentCount ]; then
/usr/bin/qbittorrent-nox --profile=/mnt/ThreeTB2/qbit_backup
fi
}

check_qbittorrent
```

cron 表达式 `*/3 * * * * /bin/ash /mnt/ThreeTB2/check_qbittorrent.sh > /tmp/crontab-qbittorrent-sh.log`

似乎是 ok 了 发现了问题所在,刚刚 使用 ps 过滤程序时 应该过滤 qbittorrent-nox 才对 所以刚刚程序中的 COUNT 会出现问题 过滤 qbittorrent-nox 就好

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/743498

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX