我在虚拟机中运行 Lubuntu 并尝试设置一个每分钟运行一次的脚本来检查 VPN 是否处于活动状态,如果不是,它会关闭所有内容并再次打开。当我手动运行脚本时,一切都按预期工作,但是当设置为 cronjob 时,它几乎每次都会失败。
首先是它无法检测路由表。当手动运行时,它可以看到隧道,但当作为 cron 运行时,它会失败并尝试退出所有内容。
第二个是它无法执行附加脚本/程序设置。它到达“启动 vpn”的点,然后退出,任何输出总是在此停止,但仅作为 cron,手动运行它启动 vpn 程序。
我似乎也无法将输出重定向到文件,除非我将每个 echo 语句附加 >>/home/localuser1/aqlog.txt。将其添加到 cron 命令中不会执行任何操作。
#!/bin/bash
PATH=/home/localuser1/bin:/home/localuser1/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
echo "-----------------------------------Script started $(date)"
echo "Check VPN"
if [route | grep tun0 -c ]; then
echo "VPN running, check qbit"
if [pgrep qbit ]; then
echo "All active, no action needed"
else
echo "Not active, starting qbit"
nohup /usr/bin/qbittorrent >/dev/null &
fi
else
echo "no VPN, killing all"
pkill qbit
pkill pia
sleep 2
echo "Starting VPN"
nohup /home/localuser1/.pia_manager/pia_manager/run.sh >/dev/null &
echo "Waiting for VPN to start"
sleep 15
echo "Check VPN active"
if [route | grep tun0 -c]; then
echo "VPN active, starting qbit"
nohup /usr/bin/qbittorrent >/dev/null &
else
echo "No VPN, end of script"
fi
fi
echo "Exiting script"
我还在 crontab 中进行了以下设置,其中 autoquit.sh 是上面脚本的任意名称:
# m h dom mon dow command
* * * * * /home/localuser1/autoquit.sh
答案1
好吧,多亏了 Reddit 上一位乐于助人的用户,我才找到了答案,所以我想在这里发帖,以防有人遇到同样的障碍。问题是我试图通过 cron 启动一个 GUI 程序,它没有传递 ENV var,这意味着它无法知道在哪里显示。一旦我通过添加 DISPLAY=:0这个有用的论坛它完全按照预期工作,现在启动程序。
固定代码如下:
#!/bin/bash
PATH=/home/localuser1/bin:/home/localuser1/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
echo "-----------------------------------Script started $(date)" >>/home/localuser1/aqlog.txt
echo "Checking VPN status" >>/home/localuser1/aqlog.txt
vpnstatus="$(route | grep tun0 -c)"
if [ "$vpnstatus" -gt "0" ]; then
echo "VPN is running, checking qbit" >>/home/localuser1/aqlog.txt
qbitstatus="$(pgrep qbit)"
if [ -n "$qbitstatus" ]; then
echo "All active, no action needed" >>/home/localuser1/aqlog.txt
else
echo "Not active, starting qbit" >>/home/localuser1/aqlog.txt
nohup qbittorrent >/dev/null &
fi
else
echo "no VPN, killing all" >>/home/localuser1/aqlog.txt
pkill qbit
pkill pia
sleep 2
echo "Starting VPN" >>/home/localuser1/aqlog.txt
nohup /home/localuser1/.pia_manager/pia_manager/run.sh >/dev/null &
echo "Waiting for VPN to start" >>/home/localuser1/aqlog.txt
sleep 15
echo "Checking if VPN is active" >>/home/localuser1/aqlog.txt
vpnstatustwo="$(route | grep tun0 -c)"
if [ "$vpnstatustwo" -gt "0" ]; then
echo "VPN is active, starting qbit" >>/home/localuser1/aqlog.txt
nohup qbittorrent >/dev/null &
else
echo "No VPN, end of script" >>/home/localuser1/aqlog.txt
fi
fi
echo "Exiting script" >>/home/localuser1/aqlog.txt
修复了 cron 作业:
# m h dom mon dow command
* * * * * export DISPLAY=:0 && /home/localuser1/autoquit.sh >> /home/localuser1/script_output.txt 2>&1