全球主机交流论坛

标题: sh脚本直接执行返回结果正常,一使用crontab就不正常 [打印本页]

作者: skywing    时间: 2022-12-1 21:41
标题: sh脚本直接执行返回结果正常,一使用crontab就不正常
本帖最后由 skywing 于 2022-12-1 21:44 编辑
  1. #!/usr/bin/env bash
  2. get_ip() {
  3.         new_ip=`dig domain.com +noall +answer | awk '{print $5}'`
  4.         old_ip=`iptables -L | grep dpt:443 | grep ACCEPT | awk '{print $4}'`
  5. }

  6. check_ip () {
  7.         get_ip
  8.         echo "the new ip is $new_ip"
  9.         echo "the old ip is $old_ip"
  10.         if [[ "${new_ip}" != "${old_ip}" ]]; then
  11.         iptables -I INPUT -s $new_ip -p tcp --dport 443 -j ACCEPT
  12.         iptables -D INPUT -s $old_ip -p tcp --dport 443 -j ACCEPT
  13.         echo "ip changed, update the ip."
  14. else
  15.         echo "ip not change."
  16.         fi
  17. }
  18. check_ip
复制代码


直接执行这个脚本,输出
  1. the new ip is xxx
  2. the old ip is xxx
  3. ip not change.
复制代码

一放到crontal里定时执行,输出
  1. the new ip is xxx
  2. the old ip is
  3. ip changed, update the ip.
复制代码

old_ip 直接返回空值
这是怎么回事呢?
作者: HOH    时间: 2022-12-1 21:44
ipt在sbin里
作者: lifetyper    时间: 2022-12-1 21:45
提示: 作者被禁止或删除 内容自动屏蔽
作者: skywing    时间: 2022-12-1 21:54
HOH 发表于 2022-12-1 21:44
ipt在sbin里

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

为什么会报这个错误呢?


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

看乐了

人家说的是 iptables 在 sbin
作者: zxxx    时间: 2022-12-1 22:37
/usr/sbin/iptables ...




欢迎光临 全球主机交流论坛 (https://hostloc.9hanju.com/) Powered by Discuz! X3.4