需求:
脚本:
#!/bin/bash scripts_name=$(basename $0) scripts_dir=$(cd `dirname $0`;pwd) log_file=/var/log/${scripts_name}.log #线程数 thred=50 #注意:如果是一个参数测不需要引号 url=" https://earth https://venus https://jupiter https://mercury " echo "===========================" >> $log_file echo "Start: $(date +"%Y-%m-%d %H:%M.%S")" >> $log_file #创建管道 [ -e /tmp/fd1 ] || mkfifo /tmp/fd1 exec 3<>/tmp/fd1 rm -f /tmp/fd1 #控制线程,为并发线程创建相应个数的占位 { for (( i = 1 ;i<=${thred};i++ )) do echo; done } >&3 #将占位信息写入管道 for i in ${url[@]} do read -u3 #读取一行,即fd1中的一个占位符 { n=`curl -sk ${i}/block?height= |egrep height|head -n1|sed 's#\"height\"\:\ ##g'|sed 's#,##g'|sed 's#\ ##g'` sleep 10 p=`curl -sk ${i}/block?height= |egrep height|head -n1|sed 's#\"height\"\:\ ##g'|sed 's#,##g'|sed 's#\ ##g'` diffheight() { if [ $n -lt $p ] ;then echo "ok,url:${i},height:$n,new height:$p" >> $log_file else echo "no,height:$n,new height:$p" >> $log_file echo -e "msg 6x_zabbix_zhulian_exigence 特别注意:${i} 1分钟没有出新块:height:$n,new height:$p" | nc localhost 8890 fi echo >&3 #任务执行完后在fd1种写入一个占位符 } if [ "$n" -gt 0 ] 2>/dev/null ;then diffheight else echo "no,url:${i},height:$n,new height:$p" >> $log_file echo -e "msg 6x_zabbix_zhulian_exigence 特别注意:${i} 获取区块高度错误:height:$n,new height:$p" | nc localhost 8890 fi } & #&表示该部分命令任务并行处理 done <&3 #指定fd1为整个for的标准输入 wait #等待所有在此shell脚本中启动的后台任务完成 exec 3>&- #关闭管道 echo "Stop: $(date +"%Y-%m-%d %H:%M.%S")" >> $log_file