sh脚本直接执行返回结果正常,一使用crontab就不正常

61次阅读

共计 896 个字符,预计需要花费 3 分钟才能阅读完成。

#!/usr/bin/env bash
get_ip() {
new_ip=`dig domain.com +noall +answer | awk ‘{print $5}’`
old_ip=`iptables -L | grep dpt:443 | grep ACCEPT | awk ‘{print $4}’`
}
check_ip () {
get_ip
echo "the new ip is $new_ip"
echo "the old ip is $old_ip"
if [["${new_ip}" != "${old_ip}" ]]; then
iptables -I INPUT -s $new_ip -p tcp –dport 443 -j ACCEPT
iptables -D INPUT -s $old_ip -p tcp –dport 443 -j ACCEPT
echo "ip changed, update the ip."
else
echo "ip not change."
fi
}
check_ip
复制代码
直接执行这个脚本,输出
the new ip is xxx
the old ip is xxx
ip not change.
复制代码
一放到 crontal 里定时执行,输出
the new ip is xxx
the old ip is
ip changed, update the ip. 复制代码
old_ip 直接返回空值
这是怎么回事呢?

ipt 在 sbin 里楼上应该是正解,这种脚本我都用绝对路径。

HOH 发表于 2022-12-1 21:44
ipt 在 sbin 里

第一行写成 #/usr/sbin bash 这个脚本就无法通过文件路径直接执行,提示如下错误
-bash: ./ip.sh: /usr/sbin: bad interpreter: Permission denied
必须前面使用 bash 才能正常执行
为什么会报这个错误呢?

skywing 发表于 2022-12-1 21:54
第一行写成 #/usr/sbin bash 这个脚本就无法通过文件路径直接执行,提示如下错误
-bash: ./ip.sh: /usr/sb …

看乐了
人家说的是 iptables 在 sbin/usr/sbin/iptables …

正文完
 0