'Shell script not running via crontab, but runs fine manually

I have a script that checks if the PPTP VPN is running, and if not it reconnects the PPTP VPN. When I run the script manually it executes fine, but when I make a cron job, it's not running.

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

Here is the script:

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


Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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; 

Solution 4:[4]

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

Solution 5:[5]

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

Solution 6:[6]

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.

Solution 7:[7]

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

Solution 8:[8]

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

Solution 9:[9]

The problem statement is script is getting executed when run manually in the shell but when run through cron, it gives "java: command not found" error -

Please try below 2 options and it should fix the issue -

  1. Ensure the script is executable .If it's not, execute below - chmod a+x your_script_name.sh

  2. The cron job doesn’t run with the same user with which you are executing the script manually - so it doesn't have access to the same $PATH variable as your user which means it can't locate the Java executable to execute the commands in the script. We should first fetch the value of PATH variable as below and then set it(export) in the script -

echo $PATH can be used to fetch the value of PATH variable.

and your script can be modified as below - Please see second line starting with export

#!/bin/sh
export PATH=<provide the value of echo $PATH>
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$$'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi

Solution 10:[10]

First of all, check if cron service is running. You know the first question of the IT helpdesk: "Is the PC plugged in?".

Solution 11:[11]

In my case, it could be solved by using this:

* * * * * root ( cd /directory/of/script/ && /directory/of/script/scriptItself.sh )

I used some ./folder/-references in the script, which didn't work.

Solution 12:[12]

For me, this was happening because the cronjob was executing from /root directory but my shell script (a script to pull the latest code from GitHub and run the tests) were in a different directory. So, I had to edit my script to have a cd to my scripts folder. My debug steps were

  1. Verified that my script run independent of cron job
  2. Checked /var/log/cron to see if the cron jobs are running. Verified that the job is running at the intended time
  3. Added an echo command to the script to log the start and end times to a file. Verified that those were getting logged but not the actual commands
  4. Logged the result of pwd to the same file and saw that the commands were getting executed from /root
  5. Tried adding a cd to my script directory as the first line in the script. When the cron job kicked off this time, the script got executed just like in step 1.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lalu
Solution 2 AndyFaizan
Solution 3 Paul Roub
Solution 4 Swati
Solution 5
Solution 6
Solution 7
Solution 8
Solution 9
Solution 10 István Berzi
Solution 11 MaestroGlanz
Solution 12 AJC