用于更新Deluged Interface IP的Bash脚本无法作为cron作业运行,但在手动运行时可以正常运行

时间:2016-09-11 17:35:28

标签: linux bash crontab

我正在运行Debian Linux变体,OpenVPN和Deluge。 我的VPN提供商拥有短的IP租约,所以每隔几天我就需要更新Deluge的接口IP,以便只在tun0上下载。我整理了一个脚本来自动执行此操作 - 它基本上将当前的tun0 IP放入$ tun0ip,然后对deluge守护程序配置文件执行grep检查以查看该字符串是否存在(这是一种脏的方法,但我猜它有效。)

我的问题是这样的:当我手动调用脚本时,它按预期工作 - 它杀死了洪水,然后重新启动它,用deluged -i $tun0ip指定新的IP。但是,当我将脚本作为cron作业运行时,它会失败 - 它将null或零值传递给$ tun0ip,然后deluged -i $tun0ip在没有指定有效IP的情况下无法工作,因此应用程序无法发射。下面的脚本。我在这里做错了吗?我真的很感激任何帮助!

#!/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

1 个答案:

答案 0 :(得分:0)

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

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