Shell script not running via crontab, but runs fine manually

Viewed 85317

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
13 Answers

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

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; 

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

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

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

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

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.

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.

my case:

crontab -e

then adding the line:

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

in fact, if I added "root" as per the user, it thought "root" was a command, and it didn't work.

Related