7

I have a pptp VPN link dialing from my (debian) linux server here, to an ISA2003 server at a remote site.

The internet connection at (both) ends is somewhat dodgy, so I have written a little script to call pon myvpnlink if it detects the link has gone down.

This script works fine if I run it from the CLI, but if its called via cron then the pon call seems to fail.

I have re-written this in both bash and perl and have the same problem with each. The script is executed, it can tell if the VPN link is currently up or down, it calls pon which calls pppd, which fails to connect. If I then go an execute the script from the CLI its fine.

Any thoughts would be very much appreciated -this is doing my box in now.

#Syslog (on a failure):
Dec 30 17:02:01 LogServer /USR/SBIN/CRON[4530]: (root) CMD (/bin/bash /root/scripts/vpnup.bash >> /root/scripts/vpn.log)
Dec 30 17:02:01 LogServer pppd[4542]: pppd 2.4.4 started by root, uid 0
Dec 30 17:02:01 LogServer pppd[4542]: Couldn't get channel number: Input/output error
Dec 30 17:02:01 LogServer pppd[4542]: Exit.

#Cron Line
* * * * * root /bin/bash /root/scripts/vpnup.bash >> /root/scripts/vpn.log

Update:

Works fine now. I was calling my cron entry via /etc/cron.d/vpn which originally looked like:

MAILTO=root
* * * * * root /bin/bash /root/scripts/vpnup.pl >> /root/scripts/vpn.log

Now that its been changed, pptp works perfectly :-D

MAILTO=root
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * root perl /root/scripts/vpnup.pl >> /root/scripts/vpn.log

many thanks to Justin for giving examples :)

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
BuildTheRobots
  • 842
  • 5
  • 11

2 Answers2

6

The error message you posted sort of points to a bug in pppd, however the situation you described "works from shell but not from cron" is usually related to path issues.

since it can't hurt, can you try adding this to the top of your crontab?

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

edit: one workaround you could use, is if it always works from the shell, have another short script that does a

while true;do vpnup.sh;sleep 1m;done >> log

then you can just start that in screen with something like

screen -S vpn -d -m thatscript
Justin
  • 3,776
  • 15
  • 20
  • 1
    Thanks for the response. I will try adding path statements into the cron entry for my script and see if there is any change. The idea of creating a loop is a good workaround, though I was trying to avoid it in case my script crashed or whatever. I like that you've called my script from another script which should add some resilience. I was wondering if I should be creating my own daemon (to run via init) that monitors the VPN, or would that just be overkill? – BuildTheRobots Jan 02 '10 at 23:39
  • 1
    yeah, the path statement helps (and calling it via both bash and perl probably helps too) -thank you :-D – BuildTheRobots Jan 03 '10 at 00:01
  • Cool :-) A daemon is probably overkill as long as cron works. I tend to use daemontools or for things that have to be running 24/7 or things that have to run in say, 10 minute intervals, but might regularly take longer than 10 minutes to run. – Justin Jan 03 '10 at 00:05
2

Thanks this worked for me! My config now looks like this...

#!/bin/bash
result=`ifconfig | grep ppp0 | wc -l`
if [[ $result  != "1" ]]; then
pon hma
sleep 10
route add -net 0.0.0.0 ppp0 #Routes all traffic on the server through VPN (useful)
fi

And my cron...

# m h  dom mon dow   command
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /home/foo/restart_vpn.sh
Chris
  • 21
  • 1