Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script not running via crontab, but runs fine manually

Tags:

bash

shell

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
like image 958
user3101956 Avatar asked Dec 14 '13 10:12

user3101956


People also ask

Why crontab scripts are not working?

One of the most frequent causes for the crontab job not being correctly executed is that a cronjob does not run under the user's shell environment. Another reason can be – not specifying the absolute path of the commands used in the script.

Why is my crontab entry not running?

Check permissionsAnother user might have created the cron job and you may not be authorized to execute it. Note that the root must own jobs added as files in a /etc/cron. */ directory. That means that if you don't have root permissions, you might not be able to access the job.

How do you test if a crontab is working?

To check to see if the cron daemon is running, search the running processes with the ps command. The cron daemon's command will show up in the output as crond. The entry in this output for grep crond can be ignored but the other entry for crond can be seen running as root. This shows that the cron daemon is running.


3 Answers

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.

like image 59
user3101956 Avatar answered Oct 09 '22 09:10

user3101956


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

like image 8
AndyFaizan Avatar answered Oct 09 '22 09:10

AndyFaizan


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; 
like image 8
Joao Vitor Deon Avatar answered Oct 09 '22 09:10

Joao Vitor Deon