2012-11-08 28 views
1

我写了一个脚本,通过ssh'ing获取每个服务器的服务器列表的加载和mem信息。但是,由于大约有20台服务器,因此等待脚本结束效率不高。这就是为什么我认为制作一个将脚本输出写入文件的crontab可能很有趣,所以我需要做的就是在需要知道20个服务器的加载和内存信息时捕获此文件。但是,当我在执行crontab期间捕获这个文件时,它会给我提供不完整的信息。这是因为我的脚本输出逐行写入文件,而不是一次终止。我不知道需要做,以使这项工作有什么...stdout一次,而不是一行一行

我的crontab:

* * * * * (date;~/bin/RUP_ssh) &> ~/bin/RUP.out 

我的bash脚本(RUP_ssh):

for comp in `cat ~/bin/servers`; do 
    ssh $comp ~/bin/ca 
done 

感谢,

niefpaarschoenen

+2

你可能想看看[Parallel ssh(pssh)](http://www.theether.org/pssh/)。 – dogbane

回答

2

您可以将输出缓存到临时文件,然后像这样一次输出:

outputbuffer=`mktemp` # Create a new temporary file, usually in /tmp/ 
trap "rm '$outputbuffer'" EXIT # Remove the temporary file if we exit early. 
for comp in `cat ~/bin/servers`; do 
    ssh $comp ~/bin/ca >> "$outputbuffer" # gather info to buffer file 
done 
cat "$outputbuffer" # print buffer to stdout 
# rm "$outputbuffer" # delete temporary file, not necessary when using trap 
+1

您需要使用'>>',否则每个ssh命令都会覆盖临时文件。或者将输出重定向移至'done'之后。 – Barmar

+0

哎呀,现在修好了。 –

+1

Tidier仍然是'trap'rm'$ outputbuffer'“EXIT'。 –

0

假设有一个字符串标识MEM /加载数据已经来到该主机的每个结果进来,你可以更新您的TXT文件。Asuming数据块是一个线长,你可以使用

for comp in `cat ~/bin/servers`; do 
    output=$(ssh $comp ~/bin/ca) 
    # remove old mem/load data for $comp from RUP.out 
    sed -i '/'"$comp"'/d' RUP.out # this assumes that the string "$comp" is 
            # integrated into the output from ca, and 
            # not elsewhere 
    echo "$output" >> RUP.out 
done 

这可以根据ca的输出进行调整。网络上有很多sed的帮助。

相关问题