0

Im running Lubuntu in a VM and trying to setup a script that runs every minute to check if the VPN is active, if not it turns everything off and on again. When i run the script manually everything works as intended, but when setup as a cronjob it fails at almost every turn.

First is that it cannot detect the routing table. When run manually it can see the tunnel but when run as a cron it fails and trys to quit everything.

The second is that it cannot execute the additional scripts/programs setup. It gets to the point of 'starting vpn' and then just quits, any output always stops here, but only as a cron, run manually it starts the vpn program.

I also cant seem to get the output to redirect to a file unless i append each echo statement with >>/home/localuser1/aqlog.txt. Adding that to the cron command does nothing.

#!/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"   

I also have the following setup in crontab, where autoquit.sh is the arbitrary name of the script above:

# m h  dom mon dow   command
* * * * * /home/localuser1/autoquit.sh
  • 1
    Don't put brackets [...] around commands if you don't understand what they're supposed to do. In particular, I doubt "everything works" when you're running this manually. – Satō Katsura May 30 '17 at 5:30
  • Thanks for the condescending comment that offered no real help. While i do understand the commands that i put inside of them from reading the man pages while i was building this, the brackets portion is simply the formatting i came across on dozens of sites when looking up the if-then-else format in bash. Im familiar with other programming languages that have parenthesis in the same place and based on the broad use of them on tutorial pages i assumed they were a format of the if function. If you want to actually be helpful and tell me WHY no brackets, that would be great. – HelloWorld3344 May 30 '17 at 16:47
  • You asked how to fix it, preferably by copying & pasting, not why it doesn't work. shrug [ is an alias for test. Consequently, (1) [ should always be followed by a space (you don't happen to have a command named [route, do you), and (2) if works by checking the exit code of the test command. Try this: if echo foo | grep -q foo; then echo found foo; fi. Which in turn works because the exit code of echo foo | grep -q foo is the exit code of grep. – Satō Katsura May 30 '17 at 21:03
0

Well, thanks to a helpful user on Reddit i was able to figure it out so i thought id post here in case anyone runs into the same obstacle. The problem is that i was trying to start a GUI program through cron, which does not have ENV var passed through, meaning it had no way of knowing where to display. As soon as i added DISPLAY=:0 via this helpful forum it worked exactly as intended and now starts the programs.

Fixed code below:

#!/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

Fixed cron job:

# m h  dom mon dow   command
* * * * * export DISPLAY=:0 && /home/localuser1/autoquit.sh >> /home/localuser1/script_output.txt 2>&1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.