Lubuntu cronjob;無法運行其他腳本或查看路線

Lubuntu cronjob;無法運行其他腳本或查看路線

我在虛擬機器中執行 Lubuntu 並嘗試設定一個每分鐘運行一次的腳本來檢查 VPN 是否處於活動狀態,如果不是,它會關閉並再次開啟所有內容。當我手動運行腳本時,一切都按預期工作,但是當設置為 cronjob 時,它幾乎每次都會失敗。

首先是它無法偵測路由表。當手動運行時,它可以看到隧道,但當作為 cron 運行時,它會失敗並嘗試退出所有內容。

第二個是它無法執行附加腳本/程式設定。它到達「啟動 vpn」的點,然後退出,任何輸出總是在此停止,但僅作為 cron,手動執行它啟動 vpn 程式。

我似乎也無法將輸出重定向到文件,除非我將每個 echo 語句附加 >>/h​​ome/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

相關內容