我编写了一个监控比特币挖掘设备的bash程序,它每秒运行一组循环,直到我监控了8个以上的单位。我的解决方案是集成并行处理,这似乎“引起了比修复更多的问题,我现在看到的是,我的所有数据都将只从一个单元记录,否则程序会因为变量而出错记录显示为空。首先,我将发布原始脚本并删除所有个人信息,然后我将尝试并行处理以查看是否有人有解决方案。谢谢你的帮助。
#!/bin/bash
# RPC the summary data
ti2=0
t=1 #Time Between Scans In Seconds
zero() {
SUMMAC=0
SUMMRJ=0
SUMMUT=0
GHs=0
SUMMHS=0
SUMMGW=0
SUMMRF=0
SUMMLW=0
SUMMDW=0
SUMMFB=0
SUMMHW=0
SUMMNB=0
SUMMET=0
}
id=(`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
go
SELECT COUNT(*) FROM Mining_Rigs
go
quit
EOF`)
for (( c=0; c<=$id-1; c++ ))
do
IPI=`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
SELECT IP_Address FROM Mining_Rigs where Row = $c
go
quit
EOF`
IP[$c]=$IPI
IPI=`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
SELECT Port FROM Mining_Rigs Where Row = $c
go
quit
EOF`
Port[$c]=$IPI
IPI=`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
SELECT Name FROM Mining_Rigs where Row = $c
go
quit
EOF`
Name[$c]=$IPI
IPI=(`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
SELECT Active FROM Mining_Rigs where Row = $c
go
quit
EOF`)
Active[$c]=$IPI
if [ ${Active[$c]} -eq 1 ]; then
l_TELNET=`echo "quit" | telnet ${IP[$c]} ${Port[$c]} | grep "Escape character is"`
if [ "$?" -ne 0 ]; then
echo "Connection to ${IP[$c]} on port ${Port[$c]} failed"
echo "${Name[$c]} Will Be Made Inactive Until Service Is Reloaded"
sleep 2
tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
UPDATE Mining_Rigs SET Active = 0 WHERE Row = $c
go
quit
EOF
Active[$c]=0
fi
fi
done
ti=$(date +%s)
while :
do
DOY=$(date +%s)
for (( c=0; c<=$id-1; c++ ))
do
if [ ${Active[$c]} -eq 1 ]; then
echo "{\"command\":\"summary\"}" | nc ${IP[$c]} ${Port[$c]} | sed 's/\x0$//' | python -mjson.tool 2> /dev/null > sum.txt
for NODE in "${IP[$c]}"; do
while read cline ; do
#DEBUGGING=$(echo $cline | awk '{ print "1="$1, "2="$2, "3="$3; exit; }' | sed 's/[:,\"]//g' )
#echo "$NODE: $DEBUGGING"
FLD1=$(echo $cline | awk '{ print $1; exit; }' | sed 's/[:,\"]//g' )
#
if [ "$FLD1" == "Getworks" ] ; then
SUMMGW=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g')
elif [ "$FLD1" == "Accepted" ] ; then
SUMMAC=$(echo $cline | awk '{ printf($2); echo ; exit; }' | sed 's/[:,\"]//g')
elif [ "$FLD1" == "Rejected" ] ; then
SUMMRJ=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g')
elif [ "$FLD1" == "Remote" ] ; then
SUMMRF=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Utility" ] ; then
SUMMUT=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "MHS" ] ; then
SUMMHS=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Local" ] ; then
SUMMLW=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Discarded" ] ; then
SUMMDW=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Found" ] ; then
SUMMFB=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Hardware" ] ; then
SUMMHW=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Network" ] ; then
SUMMNB=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Elapsed" ] ; then
SUMMET=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g' )
fi
done < sum.txt
if [ $(echo "scale=0; ($SUMMHS) / 1" | bc) -gt 0 ]; then
GHs=$(echo "scale=9; ($SUMMHS) / 1000" | bc)
fi
if [ $DOY -gt $ti ]; then
midate=`date -d "UTC 1970-01-01 $ti secs" "+%Y-%m-%d %H:%M:%S"`
tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
insert into "${Name[$c]}" (Date,Node,Accepted,Rejected,Utility,GHs,Getworks,Remfail,Local,Discard,Found,Hwerr,Network,Uptime) values( '$midate','$NODE','$SUMMAC','$SUMMRJ','$SUMMUT','$GHs','$SUMMGW','$SUMMRF','$SUMMLW','$SUMMDW','$SUMMFB','$SUMMHW','$SUMMNB','$SUMMET');
go
quit
EOF
zero
ti2=1
elif [ $DOY -eq $ti ]; then
midate=`date -d "UTC 1970-01-01 $DOY secs" "+%Y-%m-%d %H:%M:%S"`
tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
insert into "${Name[$c]}" (Date,Node,Accepted,Rejected,Utility,GHs,Getworks,Remfail,Local,Discard,Found,Hwerr,Network,Uptime) values( '$midate','$NODE','$SUMMAC','$SUMMRJ','$SUMMUT','$GHs','$SUMMGW','$SUMMRF','$SUMMLW','$SUMMDW','$SUMMFB','$SUMMHW','$SUMMNB','$SUMMET');
go
quit
EOF
zero
ti2=0
else
tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
insert into "${Name[$c]}" (Date,Node,Accepted,Rejected,Utility,GHs,Getworks,Remfail,Local,Discard,Found,Hwerr,Network,Uptime) values( FROM_UNIXTIME('$DOY') ,'$NODE','$SUMMAC','$SUMMRJ','$SUMMUT','$GHs','$SUMMGW','$SUMMRF','$SUMMLW','$SUMMDW','$SUMMFB','$SUMMHW','$SUMMNB','$SUMMET');
EOF
zero
fi
done
fi
done
if [ $ti2 -eq 1 ]; then
ti3=$(expr $DOY - $ti )
t2=$(expr $t - $ti3 )
if [ $t2 -ge 0 ]; then
ti=$(expr $DOY + $t2 )
sleep $t2
fi
elif [ $ti2 -eq 0 ]; then
ti=$(expr $DOY + $t )
sleep $t
fi
done
这是我原来的程序就像一个魅力,直到我有8个单位,接下来是我尝试并行处理
#!/bin/bash
# RPC the summary data
ti2=0
t=3 #Time Between Scans In Seconds
Json() {
zero() {
SUMMAC=0
SUMMRJ=0
SUMMUT=0
GHs=0
SUMMHS=0
SUMMGW=0
SUMMRF=0
SUMMLW=0
SUMMDW=0
SUMMFB=0
SUMMHW=0
SUMMNB=0
SUMMET=0
}
c=$d
if [ ${Active[$c]} -eq 1 ]; then
echo "{\"command\":\"summary\"}" | nc -w 1 ${IP[$c]} ${Port[$c]} | sed 's/\x0$//' | python -mjson.tool 2> /dev/null > sum.txt
for NODE in "${IP[$c]}"; do
while read cline ; do
#DEBUGGING=$(echo $cline | awk '{ print "1="$1, "2="$2, "3="$3; exit; }' | sed 's/[:,\"]//g' )
#echo "$NODE: $DEBUGGING"
FLD1=$(echo $cline | awk '{ print $1; exit; }' | sed 's/[:,\"]//g' )
#
if [ "$FLD1" == "Getworks" ] ; then
SUMMGW=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g')
elif [ "$FLD1" == "Accepted" ] ; then
SUMMAC=$(echo $cline | awk '{ printf($2); echo ; exit; }' | sed 's/[:,\"]//g')
elif [ "$FLD1" == "Rejected" ] ; then
SUMMRJ=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g')
elif [ "$FLD1" == "Remote" ] ; then
SUMMRF=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Utility" ] ; then
SUMMUT=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "MHS" ] ; then
SUMMHS=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Local" ] ; then
SUMMLW=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Discarded" ] ; then
SUMMDW=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Found" ] ; then
SUMMFB=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Hardware" ] ; then
SUMMHW=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Network" ] ; then
SUMMNB=$(echo $cline | awk '{ printf($3); exit; }' | sed 's/[:,\"]//g' )
elif [ "$FLD1" == "Elapsed" ] ; then
SUMMET=$(echo $cline | awk '{ printf($2); exit; }' | sed 's/[:,\"]//g' )
fi
done < sum.txt
echo $SUMMHS
echo $SUMMUT
if [ $(echo "scale=0; ($SUMMHS) / 1" | bc) -gt 0 ]; then
GHs=$(echo "scale=9; ($SUMMHS) / 1000" | bc)
fi
if [ $DOY -gt $ti ]; then
midate=`date -d "UTC 1970-01-01 $ti secs" "+%Y-%m-%d %H:%M:%S"`
zero
ti2=1
elif [ $DOY -eq $ti ]; then
midate=`date -d "UTC 1970-01-01 $DOY secs" "+%Y-%m-%d %H:%M:%S"`
tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
insert into "${Name[$c]}" (Date,Node,Accepted,Rejected,Utility,GHs,Getworks,Remfail,Local,Discard,Found,Hwerr,Network,Uptime) values( '$midate','$NODE','$SUMMAC','$SUMMRJ','$SUMMUT','$GHs','$SUMMGW','$SUMMRF','$SUMMLW','$SUMMDW','$SUMMFB','$SUMMHW','$SUMMNB','$SUMMET');
go
quit
EOF
zero
ti2=0
else
tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
insert into "${Name[$c]}" (Date,Node,Accepted,Rejected,Utility,GHs,Getworks,Remfail,Local,Discard,Found,Hwerr,Network,Uptime) values( FROM_UNIXTIME('$DOY') ,'$NODE','$SUMMAC','$SUMMRJ','$SUMMUT','$GHs','$SUMMGW','$SUMMRF','$SUMMLW','$SUMMDW','$SUMMFB','$SUMMHW','$SUMMNB','$SUMMET');
EOF
zero
fi
done
fi
}
id=(`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
go
SELECT COUNT(*) FROM Mining_Rigs
go
quit
EOF`)
for (( c=0; c<=$id-1; c++ ))
do
IPI=`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
SELECT IP_Address FROM Mining_Rigs where Row = $c
go
quit
EOF`
IP[$c]=$IPI
IPI=`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
SELECT Port FROM Mining_Rigs Where Row = $c
go
quit
EOF`
Port[$c]=$IPI
IPI=`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
SELECT Name FROM Mining_Rigs where Row = $c
go
quit
EOF`
Name[$c]=$IPI
IPI=(`tsql -S 192.168.0.1\\MSSQLSERVER -U Username -P Password -o qh <<EOF
use DATABASE NAME
SELECT Active FROM Mining_Rigs where Row = $c
go
quit
EOF`)
Active[$c]=$IPI
done
ti=$(date +%s)
Getinfo
while :
do
DOY=$(date +%s)
for (( c=0; c<=$id-1; c++ ))
do
Json $c &
echo $c
done
if [ $ti2 -eq 1 ]; then
ti3=$(expr $DOY - $ti )
t2=$(expr $t - $ti3 )
if [ $t2 -ge 0 ]; then
ti=$(expr $DOY + $t2 )
sleep $t2
fi
elif [ $ti2 -eq 0 ]; then
ti=$(expr $DOY + $t )
sleep $t
fi
wait
done
答案 0 :(得分:0)
他们有两个问题,一个你不能在linux上一次运行多个NC命令,如果你有这个问题而不是运行Ncat。其次,我将funstion json的代码复制到一个新的bash脚本中。导出我的变量并用安培数运行脚本。解决问题