【发布时间】:2018-08-26 00:55:09
【问题描述】:
我有一个通过 openvpn 连接到 VPN 的 Raspberry Pi。连接会定期断开,因此我使用以下脚本:
#!/bin/bash
ps -ef | grep -v grep | grep openvpn
if [ $? -eq 1 ] ; then
/sbin/shutdown -r now
fi
我将它添加到 crontab(使用sudo crontab -e),我希望脚本每 5 分钟执行一次:
*/5 * * * * /etc/openvpn/check.sh
脚本不起作用,但似乎仍然每五分钟执行一次:
tail /var/log/syslog | grep CRON
给予:
Mar 16 21:15:01 raspberrypi CRON[11113]: (root) CMD (/etc/openvpn/check.sh)
...
此外,当我使用 sudo ./check.sh 手动运行脚本时,Pi 会正常重启。
我真的不明白这里发生了什么?
编辑:
按照建议,我添加了完整路径名,然后从重启 Pi 到重启 openvpn:
#!/bin/bash
if ! /bin/ps -ef | /bin/grep '[o]penvpn'; then
cd /etc/openvpn/
/usr/sbin/openvpn --config /etc/openvpn/config.ovpn
fi
该脚本仍然无法运行,尽管我自己执行它时它运行良好。脚本的权限是755,应该没问题吧?
【问题讨论】:
-
考虑使用
killall -0 openvpn。如果该过程存在,您将获得 $?等于零。也许是权限问题,所以您编辑的 crontab 的用户不允许执行关机操作。尝试将bash -x /etc/openvpn/check.sh 2>&1 | logger -t OPENVPNCHECK放入 crontab。 -
您可能需要在 check.sh 中设置路径,因为 cron 不使用用户 PATH 运行,或者使用绝对路径。
-
@frist 我用了
sudo crontab -e(对不起,应该已经在问题中澄清了),所以我认为应该没有任何权限问题? -
为什么要重启?!不能只是重新连接 vpn 吗?
-
在脚本中,添加二进制文件的完整路径,如 /bin/ps 等(使用 'which grep' 等命令获取它们)。