我是一个R用户。我总是在校园的多台计算机上运行程序。例如,我需要运行10个不同的程序。我需要打开PuTTY 10次才能登录到10台不同的计算机。并将每个程序提交给10台计算机(它们的操作系统是Linux)。有没有办法登录10台不同的计算机,并同时发送命令?我使用以下命令提交程序
nohup Rscript L_1_cc.R > L_1_sh.txt
nohup Rscript L_2_cc.R > L_2_sh.txt
nohup Rscript L_3_cc.R > L_3_sh.txt发布于 2015-04-14 22:57:36
首先设置ssh,这样您就可以在不输入密码的情况下登录(如果您不知道如何登录,请使用google)。然后将脚本写入ssh到每个远程主机以运行命令。下面是一个例子。
#!/bin/bash
host_list="host1 host2 host3 host4 host5 host6 host7 host8 host9 host10"
for h in $host_list
do
case $h in
host1)
ssh $h nohup Rscript L_1_cc.R > L_1_sh.txt
;;
host2)
ssh $h nohup Rscript L_2_cc.R > L_2_sh.txt
;;
esac
done这是一个非常简单的例子。您可以做得更好(例如,您可以将".R“和".txt”文件名放入变量中并使用它,而不是显式地列出在本例中的每个选项)。
发布于 2015-04-14 23:20:48
基于您的主题标记,我假设您正在使用ssh登录到远程机器。希望您使用的机器是基于*nix的,这样您就可以使用以下脚本。如果您在Windows上,请考虑使用cygwin。
首先,阅读本文,在每个远程目标上设置公钥身份验证:http://www.cyberciti.biz/tips/ssh-public-key-based-authentication-how-to.html
这将防止ssh在每次登录到每个目标时提示您输入密码。然后,可以对每个目标编写命令执行脚本,如下所示:
#!/bin/bash
#kill script if we throw an error code during execution
set -e
#define hosts
hosts=( 127.0.0.1 127.0.0.1 127.0.0.1)
#define associated user names for each host
users=( joe bob steve )
#counter to track iteration for correct user name
j=0
#iterate through each host and ssh into each with user@host combo
for i in ${hosts[*]}
do
#modify ssh command string as necessary to get your script to execute properly
#you could even add commands to transfer the file into which you seem to be dumping your results
ssh ${users[$j]}@$i 'nohup Rscript L_1_cc.R > L_1_sh.txt'
let "j=j+1"
done
#exit no error
exit 0如果设置了公钥身份验证,则只需执行脚本就可以让每个远程主机完成它们的任务。您甚至可以查看如何从文件加载用户/主机数据,以避免将这些信息硬编码到数组中。
https://stackoverflow.com/questions/29637819
复制相似问题