17

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
Laurel
  • 5,965
  • 14
  • 31
  • 57
user3101956
  • 493
  • 1
  • 4
  • 7
  • 2
    this `sed` statement will never work. The whole solution looks fragile. Use an `if-up.d` script for that – hek2mgl Dec 14 '13 at 10:40
  • Dec 14 12:41:01 server3 CRON[5381]: (root) CMD (/bin/bash /var/scripts/vpn-check.sh ) – user3101956 Dec 14 '13 at 10:41
  • @hek2mgl, why? Anyway, sed should have the full path, just as anything else. – pvgoran Dec 14 '13 at 10:41
  • The usual causes for cron jobs to not run are permissions and paths. Please read the `cron` man entry a few times and try to understand how paths break and what permissions the cron job will run under. – Bob Dalgleish Dec 14 '13 at 10:42
  • 1
    @user3101956 because the substitute command in sed looks like `s/PATTERN/REPLACEMENT/` and yours is just `s/PATTERN` – hek2mgl Dec 14 '13 at 10:43

17 Answers17

29

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.

lalu
  • 331
  • 1
  • 3
  • 10
user3101956
  • 493
  • 1
  • 4
  • 7
  • 1
    Yeah this just helped me out. `crontab -e` was the issue, on a RHEL 6.7 machine :) – sofly Jul 01 '16 at 21:03
  • 7
    This is an imprecise answer. The fundamental problem is that there are two different variants of `crontab` syntax. This answer is correct for a system-wide `crontab` owned by `root`, but what you originally had would be correct for a user's private `crontab`. – tripleee Nov 23 '17 at 08:20
  • 1
    For me only adding /bin/bash for Ubuntu 16.04 helped for crontab -e – Semih Korkmaz Feb 19 '19 at 19:43
10

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

AndyFaizan
  • 1,833
  • 21
  • 30
10

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; 
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Joao Vitor Deon
  • 117
  • 1
  • 5
3

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.

MaestroGlanz
  • 321
  • 3
  • 12
2

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
Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
2

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.

janos
  • 120,954
  • 29
  • 226
  • 236
  • the script is working without cron :) i will try this now, i mean when i run the script ./vpn-check.sh is checks and reconnect the vpn , when is on cron it doesn't reconnect it – user3101956 Dec 14 '13 at 10:57
  • same with this script , if i execute ./vpn-check.sh it checks that vpn is not running and reconnect it , when its on cron ... it doesn't – user3101956 Dec 14 '13 at 11:02
  • tail -f /var/log/syslog | grep CRON Dec 14 13:02:01 server3 CRON[5863]: (root) CMD (/bin/bash /var/scripts/vpn-check.sh) – user3101956 Dec 14 '13 at 11:02
  • content of log file is: Sat Dec 14 13:12:01 EET 2013 PING 192.168.17.27 (192.168.17.27) 56(84) bytes of data. --- 192.168.17.27 ping statistics --- 3 packets transmitted, 0 received, 100% packet loss, time 2016ms – user3101956 Dec 14 '13 at 11:11
  • Sat Dec 14 13:16:01 EET 2013 PING 192.168.17.27 (192.168.17.27) 56(84) bytes of data. --- 192.168.17.27 ping statistics --- 3 packets transmitted, 0 received, 100% packet loss, time 2016ms – user3101956 Dec 14 '13 at 11:16
  • I added more logging in the script, please try again. And please don't post the output in comments, because that's hard to read and breaks the formatting. Paste it inside your question. – janos Dec 14 '13 at 11:18
  • Again is the same, script manually is working , but cron not connecting it :) Sat Dec 14 13:28:01 EET 2013 PING 192.168.17.27 (192.168.17.27) 56(84) bytes of data. From 91.184.204.126 icmp_seq=1 Destination Host Unreachable --- 192.168.17.27 ping statistics --- 3 packets transmitted, 0 received, +1 errors, 100% packet loss, time 2000ms no pings received, calling pppd done – user3101956 Dec 14 '13 at 11:29
  • I told you to NOT paste the output here but inside your question, come on man, 3 times already! Anyway, does pppd have its own log? Because by now it is clear that the problem is with running pppd itself. Maybe it needs some environment variables or other settings that are only set in interactive shells and not with cron. If pppd has its own log that would help debugging this. If not, maybe you have some settings in your `.bashrc`, `.profile` or `.bash_profile` that is not sourced when running with cron. Please check on these points. – janos Dec 14 '13 at 11:47
2

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
Swati
  • 28,069
  • 4
  • 21
  • 41
user2954660
  • 117
  • 1
  • 3
2

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.

AMDP
  • 327
  • 2
  • 12
1

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
Thibault
  • 1,566
  • 15
  • 22
0

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
Arihant
  • 3
  • 5
0

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

0

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.
AJC
  • 981
  • 10
  • 17
0

it was timezone in my case. I scheduled cron with my local time but server has different timezone and it does not run at all. so make sure your server has same time by date cmd

0

first run command env > env.tmp then run cat env.tmp copy PATH=.................. Full line and paste into crontab -e, line before your cronjobs.

gmi rushi
  • 11
  • 1
0

In my case (ubuntu 20.04) the error was caused by /etc/crontab. It is used

run-parts --report /etc/cron.hourly 

run-parts seems to ignore all scripts with extensions dots in filename.

/etc/cron.hourly/abc was executed by cron/run-parts

/etc/cron.hourly/xyz.sh was not executed by cron/run-parts

You can debug this with the following command

run-parts --list /etc/cron.hourly 

So in my case I only needed a simple mv xyz.sh xyz.

oceanBT
  • 204
  • 6
  • 11
0

In my case, the solution was to log the output of the crontab script:

* * * * * /script/path/script.sh >> /log/path/log.log 2>&1

After that log.log file showed me that I didn't specify python virtual environment and shell script couldn't import some libraries (ImportError: No module named module_name). I fixed this and everything started to work!

So, in my opinion, it`s important to monitor your logs of this process.

Ivan
  • 23
  • 3
  • 6
-1

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

smile
  • 1
  • 2