跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • sh脚本直接执行返回结果正常,一使用crontab就不正常
未分類
1 12 月 2022

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

skywing 大佬有话说 :

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

本帖最后由 skywing 于 2022-12-1 21:44 编辑

#!/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 直接返回空值yc002t
这是怎么回事呢?

HOH 大佬有话说 :

ipt在sbin里

lifetyper 大佬有话说 :

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

skywing 大佬有话说 :

HOH 大佬有话说 : 2022-12-1 21:44
ipt在sbin里

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

为什么会报这个错误呢?

tiga 大佬有话说 :

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

看乐了

人家说的是 iptables 在 sbin

zxxx 大佬有话说 :

/usr/sbin/iptables …

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具