我有这个脚本,我用一个参数调用它。参数是一个IP地址。我想知道这个脚本的执行时间。有时只需要几秒钟,有时最多需要30分钟才能看到脚本的输出。我不知道为什么。
如果我调用脚本,等待几秒钟,然后使用ctrl + c中止脚本,我可以立即看到脚本的输出。如果我只是等待,我会看到同样的输出,但30分钟后。
你知道为什么这个脚本或者我的服务器有buggy吗?
谢谢。
#!/bin/bash
#########################################
# blocks IP address permanently via ufw #
#########################################
# check if user is root
if [[ "$EUID" -ne 0 ]]; then
  echo "Run as root. Exit."
  exit
fi
# check argument it given
ip=${1:?No IP address given. Exit.}
# IPv4 regex
regex4='^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(\/[0-9]{2}){0,1}$'
# IPv6 regex
regex6='^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}(\/[0-9]{1,2}){0,1}$'
isIP=false
if [[ $ip =~ $regex4 ]]; then
  ufw insert 1 deny from $ip
  echo "$ip blocked permanently!"
  isIP=true
elif [[ $ip =~ $regex6 ]]; then
  ufw prepend deny from $ip
  isIP=true
else
  echo "IP address is wrong."
fi
#ufw status verbose
# add blocked ip to file and commit it.
if [ "$isIP" = true ] && [[ $(grep -L "$ip" ips/custom.txt) ]] ; then
  echo $ip >> ips/custom.txt
  git add ips/custom.txt
  git commit -m "added ip to ips/custom.txt"
  git push
fi发布于 2020-01-27 23:36:21
我在我的脚本中也有同样的问题。我认为问题出在代码行echo $ip >> ips/custom.txt上,请尝试执行以下操作:
echo $ip >> ips/custom.txt 2>&1 &
让我知道。
https://stackoverflow.com/questions/59932464
复制相似问题