简体   繁体   中英

Bash script to update Deluged Interface IP fails to run as cron job, but works when run manually

I'm running a Debian Linux variant, OpenVPN, and Deluge. My VPN provider has short IP leases, so every few days I need to update Deluge's interface IP so that it downloads only on tun0. I put together a script to automate this - it basically puts the current tun0 IP into $tun0ip, then does a grep check against the deluge daemon config file to see if that string is present (this is a dirty way to do it, but I guess it works).

My problem is this: When I call the script manually, it works as intended - it kills deluged and then relaunches it, specifying the new IP with deluged -i $tun0ip . However, when I run the script as a cron job, it fails - it passes a null or zero value to $tun0ip, and then deluged -i $tun0ip doesn't work without a valid IP specified, so the application fails to launch. Script below. Am I doing something wrong here? I really appreciate any help!

#!/bin/bash

tun0ip=$( ifconfig tun0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' |   grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')

if grep -q $tun0ip "/root/.config/deluge/core.conf"; then
#Great, the IP is correct - don't have to do anything.
echo "IP Looks good - no changes were made."
else
echo "tun0 IP not found in config file. Killing deluged and rebooting with $tun0ip as interface."
killall deluged
sleep 5
deluged -i $tun0ip

fi

我必须指定/ sbin / ifconfig,如下所示:

tun0ip=$( /sbin/ifconfig tun0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM