Starting pppd from cron doesn't work

2.4k Views Asked by At

I want to start pppd whenever it disconnects. I am trying to setup a shell script to run every 1 minute to see if it's down and reconnect.

I have a bash script called vpn-check.sh:

ping -c3 10.8.3.0 > pingreport
result=`grep "0 received" pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$/\1/'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
  pon VPNname
fi

When I run this script from cli directly, it works and starts ppp but when I run the same through cronjob (for root user), it doesn't work.

I tried the below and didn't work

*/1 * * * * bash /root/vpn-check.sh > /root/cronlog.txt 2>&1

I tried the below and didn't work

*/1 * * * * /root/vpn-check.sh > /root/cronlog.txt 2>&1

Finally, I tried:

*/1 * * * * /usr/sbin/pppd call VPNname> /root/cronlog.txt 2>&1

Can't figure out what could be wrong.

4

There are 4 best solutions below

0
On

I was getting the same problem too.I used @dave's answer to figure it out. You just need to add the user name to the crontab, Add the next line to the end of /etc/crontab file:

*/1 * * * * root bash /root/vpn.sh

Replace the shell name of your own.

0
On

You are missing the shebang from your shell script.

vpn-check.sh should look like:

#!/bin/bash
ping -c3 10.8.3.0 > pingreport
result=`grep "0 received" pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$/\1/'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
pon VPNname
fi

See:

http://linuxconfig.org/bash-scripting-tutorial

What does the line "#!/bin/sh" mean in a UNIX shell script?

1
On

I fixed it. All this while I was running crontab -e but for the user name to be added, it needs to be added to the system-wide cron file found under /etc/crontab

user that starts the job can only be added in the above mentioned system wide cron file.

1
On

I still don't understand why some scripts work and don't work from cron when it is being called as the correct user according to the logs.

The only solution I found is to run:

crontab -e

and add the following lines to the top (even though I am calling the pppd daemon by the full path):

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