bash - Crontab apparently not calling sudo -
tl;dr bash script requires sudo works fine when called manually, not work when called crontab, though add crontab using sudo crontab -e
i trying automate connection vpn proxy such vpn connection turned on @ midnight , off @ 7:15am. in order automate it, wrote following bash script, located @ /usr/local/bin/cloak.sh
.
#!/bin/bash log_file=/home/declan/log/cloak.log logentry() { while read data echo "$(date "+%y %m %d %t") ; $data" >>$log_file 2>&1; done } echo "---------------------" | logentry if [ $1 -eq 1 ] echo "turning cloak on" | logentry /etc/init.d/openvpn start proxpn.miami | logentry else echo "turning cloak off" | logentry /etc/init.d/openvpn stop | logentry fi echo "---------------------" | logentry echo " " | logentry
my vpn working , have confirmed script executable , works checking ip address before , after calls script, turning vpn on (1 parameter) or off (0 parameter).
declan@mx:~/log$ wget http://ipecho.net/plain -o - -q ; echo 74.196.220.81 <<-- real ip address declan@mx:~/log$ sudo /usr/local/bin/cloak.sh 1 <<-- turn vpn on declan@mx:~/log$ wget http://ipecho.net/plain -o - -q ; echo 173.0.8.33 <<-- vpn ip address declan@mx:~/log$ sudo /usr/local/bin/cloak.sh 0 <<-- turn vpn off declan@mx:~/log$ wget http://ipecho.net/plain -o - -q ; echo 74.196.220.81 <<-- real ip address declan@mx:~/log$
i add script cron, using sudo crontab -e
ensure sudo running script, following lines in crontab
# cloak on @ midnight, off @ 7:15am 0 0 * * * /usr/local/bin/cloak.sh 1 15 7 * * * /usr/local/bin/cloak.sh 0
if @ contents of log, /home/declan/log/cloak.log
, shows cron job being executed @ correct time.
the problem when check ip address after cron job tries start vpn, real ip address still being used. guess somehow cron job not being called sudo rights, can't understand why.
any appreciated. open suggestions on alternative approaches, different scripts, etc...
thanks declan
added following crontab , working intended.
path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Comments
Post a Comment