效率更高的脚本?

时间:2016-09-03 15:31:57

标签: linux bash

我刚刚将四个Raspberry Pi 3作为一个专门的BOINC群集。我喜欢我所拥有的,但监控进度有点meh,因为我的主桌面环境是Linux(Windows具有良好的GUI,具有远程监控支持,但Linux等价物似乎已经不存在)。

无论如何,我把这个bash脚本拼凑起来跟踪事情

#!/bin/bash

tasks=($(boinccmd --get_tasks | grep ')' | cut -d ')' -f1))
progress=($(boinccmd --get_tasks | grep fraction | awk '{ print $NF }'))
remaining=($(boinccmd --get_tasks | grep remaining | awk '{ print $NF }'))

tabs -4 &> /dev/null

echo "${tasks[-1]} task(s)"

for i in "${tasks[@]}"; do
  if [ $(echo "${progress[((--i))]} > 0.000" | bc) -eq 1 ]; then
    progress_as_percent=$(echo "scale=1; (${progress[((--i))]}*100)/1" | /usr/bin/bc)
    remaining_as_hours=$(echo "scale=0; ${remaining[((--i))]}/3600" | /usr/bin/bc)
    remaining_as_minutes=$(echo "scale=0; (${remaining[((--i))]}/60)-(${remaining_as_hours}*60)" | /usr/bin/bc)
    echo -e "#$i"'\t'"${progress_as_percent}%"'\t'"${remaining_as_hours}:${remaining_as_minutes}"
  fi
done

cpu_temp="$(sudo cat /sys/class/thermal/thermal_zone0/temp)"
cpu_temp=$(echo "scale=1; ${cpu_temp}/1000" | bc)

core0freq="$(sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq)"
core0freq=$(echo "scale=2; ${core0freq}/1000000" | bc)
core1freq="$(sudo cat /sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_cur_freq)"
core1freq=$(echo "scale=2; ${core1freq}/1000000" | bc)
core2freq="$(sudo cat /sys/devices/system/cpu/cpu2/cpufreq/cpuinfo_cur_freq)"
core2freq=$(echo "scale=2; ${core2freq}/1000000" | bc)
core3freq="$(sudo cat /sys/devices/system/cpu/cpu3/cpufreq/cpuinfo_cur_freq)"
core3freq=$(echo "scale=2; ${core3freq}/1000000" | bc)

tabs -8 &> /dev/null

echo
echo "${core0freq}/${core1freq}/${core2freq}/${core3freq}GHz ${cpu_temp}'C"

输出看起来像这样:

12 task(s)
#1      33.3%   11:14
#3      29.2%   12:50
#6      31.4%   12:15
#10     31.6%   11:58

1.20/1.20/1.20/1.20GHz  46.6'C

我不知道bc与其他替代方案的效率,也不知道我确定此脚本实例化的许多shell。显然,由于RPi3在每美元FLOPS方面是一台动力不足的机器,因此这种构建并不主要关注效率......但同样重要!我想知道我能做些什么来倾向于获取我之后的信息。跟踪工作量并不是真的那么重要,但CPU温度和核心时钟都是。

提前致谢。 =)

编辑:根据请求,这里是boinccmd --get_tasks的样子:

pi@serverpi:~ $ boinccmd --get_tasks

======== Tasks ========
1) -----------
   name: p2030.20151124.G196.37-01.56.N.b3s0g0.00000_1563_1
   WU name: p2030.20151124.G196.37-01.56.N.b3s0g0.00000_1563
   project URL: http://einstein.phys.uwm.edu/
   report deadline: Sat Sep 17 01:19:42 2016
   ready to report: no
   got server ack: no
   final CPU time: 28977.680000
   state: downloaded
   scheduler state: preempted
   exit_status: 0
   signal: 0
   suspended via GUI: no
   active_task_state: UNINITIALIZED
   app version num: 142
   checkpoint CPU time: 28917.330000
   current CPU time: 28977.680000
   fraction done: 0.502852
   swap size: 127 MB
   working set size: 125 MB
   estimated CPU time remaining: 28691.772814

并且(在我的情况下)有14个独立的工作单位。

1 个答案:

答案 0 :(得分:2)

您似乎在开始时调用original_list = [1,'one',2,'two',3,'three'] numbers_list = a[::2] # start at index[0], continue through list, get every 2nd element >>> print numbers_list [1, 2, 3] strings_list = a[1::2] # start at index[1], continue through list, get every 2nd element >>> print strings_list ['one', 'two', 'three'] 三次,然后为每项任务调用boinccmd三次。因此,对于14个任务,您将有45个进程,而您可以只调用bc一次和boinccmd一次并获得相同的结果。

我不会永远为你做这一切,但它看起来很简单:

awk

<强>输出

boinccmd --get_tasks | awk -F '[):]' '
   /^[0-9])/   {tasks++}
   /fraction/  {pctdone=$2*100}
   /remaining/ {hrs=int($2/3600)
                mins=int(($2-(hrs*3600))/60)
                printf "#%d\t%2.1f\t%d:%02d\n",tasks,pctdone,hrs,mins}
   END{printf "%d task(s)\n",tasks}
   '

我添加了更多逻辑并改进了编号,所以它现在看起来像这样:

#1  50.3    7:58
#2  98.0    1:00
2 task(s)