Shell script not running via crontab, but runs fine manually

前端 未结 8 621
清歌不尽
清歌不尽 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:31

    try this /home/your site folder name/public_html/gistfile1.sh set cron path like above

    0 讨论(0)
  • 2020-12-17 01:32

    In my case, the issue was that the script wasn't marked as executable. To make sure it is, run the following command:

    chmod +x your_script.sh

    0 讨论(0)
  • 2020-12-17 01:34

    After a long time getting errors, I just did this:

    SHELL=/bin/bash 
    PATH=/bin:/sbin:/usr/bin:/usr/sbin
    * * * * * /bin/bash /home/joaovitordeon/Documentos/test.sh
    

    Where test.sh contains:

    #!/bin/bash 
    
    /usr/bin/python3  /home/joaovitordeon/Documentos/test.py; 
    
    0 讨论(0)
  • 2020-12-17 01:39

    finally i found a solution ... instead of entering the cronjob with

    crontab -e
    

    i needed to edit the crontab file directly

    nano /etc/crontab
    

    adding e.g. something like

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

    and its fine now!

    Thank you all for your help ... hope my solution will help other people as well.

    0 讨论(0)
  • 2020-12-17 01:39

    Your script can be corrected and simplified like this:

    #!/bin/sh
    log=/tmp/vpn-check.log
    { date; ping -c3 192.168.17.27; } > $log
    if grep -q '0 received' $log; then
        /usr/sbin/pppd call home >>$log 2>&1
    fi
    

    Through our discussion in comments we confirmed that the script itself works, but pppd doesn't, when running from cron. This is because something must be different in an interactive shell like your terminal window, and in cron. This kind of problem is very common by the way.

    The first thing to do is try to remember what configuration is necessary for pppd. I don't use it so I don't know. Maybe you need to set some environment variables? In which case most probably you set something in a startup file, like .bashrc, which is usually not used in a non-interactive shell, and would explain why pppd doesn't work.

    The second thing is to check the logs of pppd. If you cannot find the logs easily, look into its man page, and it's configuration files, and try to find the logs, or how to make it log. Based on the logs, you should be able to find what is missing when running in cron, and resolve your problem.

    0 讨论(0)
  • 2020-12-17 01:42

    Was having a similar problem that was resolved when a sh was put before the command in crontab

    This did not work :

    @reboot ~myhome/mycommand >/tmp/logfile 2>&1
    

    This did :

    @reboot sh ~myhome/mycommand >/tmp/logfile 2>&1
    
    0 讨论(0)
提交回复
热议问题