Shell script not running via crontab, but runs fine manually

前端 未结 8 622
清歌不尽
清歌不尽 2020-12-17 01:25

I have a script that checks if pptp vpn is running , and if not it reconnect the pptp vpn , when i run the script manually it execute fine , when i do set a cron job , is no

相关标签:
8条回答
  • 2020-12-17 01:43

    If you're positive the script runs outside of cron, execute

    printf "SHELL=$SHELL\nPATH=$PATH\n* * * * * /bin/bash /var/scripts/vpn-check.sh\n"
    

    Do crontab -e for whichever crontab you're using and replace it with output of the above command. This should mirror most of your environment in case there is some missing path issue or something else. Also check logs for any errors it's getting.

    Though it definitly looks like the script has an error or you messed something up when copying it here

    sed: -e expression #1, char 44: unterminated `s' command
    ./bad.sh: 5: ./bad.sh: [[: not found
    

    Simple alternate script

    #!/bin/bash
    
    if [[ $(ping -c3 192.168.17.27) == *"0 received"* ]]; then
      /usr/sbin/pppd call home
    fi
    
    0 讨论(0)
  • 2020-12-17 01:44

    As a complement of other's answers, didn't you forget the username in your crontab script ?

    Try this :

    * * * * * root /bin/bash /var/scripts/vpn-check.sh
    

    EDIT

    Here is a patch of your code

    #!/bin/sh
    /bin/ping -c3 192.168.17.27  > /tmp/pingreport
    result=`grep "0 received" /tmp/pingreport`
    truncresult=`echo "$result" | /bin/sed 's/^\(.................................\).*$/\1/'`
    if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
        /usr/sbin/pppd call home
    fi
    
    0 讨论(0)
提交回复
热议问题