1

I need to connect multiple computers to run different programs. For different computers, I need to send them different command. How to use ssh command to connect to multiple computers and send different commands to each computers? My command to run program for each computer is nohup Rscript program_i.R > program_i_out.txt. To be detailed. I want to use 3 computers to run 3 programs. Then, I will send

nohup Rscript program_1.R > program_1_out.txt to first computer,

nohup Rscript program_2.R > program_2_out.txt to second computer,

nohup Rscript program_3.R > program_3_out.txt to third computer.

  • Welcome to StackExchange. You might like to provide a little more detail, with examples. What different (sorts of) commands do you need to send? How can you differentiate which command needs to be run on which computer? – roaima Apr 14 '15 at 23:00
  • Also see stackoverflow.com/questions/29637819/…, which I think is the same question phrased slightly differently – roaima Apr 14 '15 at 23:36
  • There are ssh clients for this: look at mssh, mussh, clusterssh (some of these are gui). – ctrl-alt-delor Apr 15 '15 at 10:05
1

Try ssh -f user@host '<your command here>'.

From the ssh man page:

-f Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background.

For example, if i do ssh -f <my computer> 'echo "hello $(pstree -p | grep sshd | wc -l) person in ssh!", it prints hello 1 person in ssh!.

After it prints 2 the session automatically exits, but you do not have to wait for it to exit. If I do ssh -f <my computer> 'sleep 30 && echo "hello $(pstree -p | grep sshd | wc -l) person in ssh!"' it appears as if nothing has happened and I can do anything as normal, but 30 seconds later, it prints hello 1 person in ssh!.

Using the > redirect will redirect all stdout to a file, but all errors will still be printed to your screen. To avoid this, you can use &> to redirect all output, stdout and stderr, to a file.

| improve this answer | |
  • I use command ssh -f mike@mycompter_1.com 'cd/home/temp && nohup Rscript L_6.R> L_6_sh.txt &' . But it does not work: after I enter this command, it ask me to type password. And then it appear [1] 948, and it stuck there. I only can use ctrl+c to get out, – Mike Brown Apr 14 '15 at 23:40
  • @MikeBrown Try it without the ampersand at the end. If that doesn't work, try it without the nohup. – Cormac Apr 14 '15 at 23:43
  • 1
    It works now! But I have a lot of programs to assign to different computers. After typing what you suggest, it always let type password (can you help me here?), and can you help make the command as sh file (I want to assign different commands to different computers at same time). Then, each time, I only need to edit that sh file and run it. I am a new user of Linux. – Mike Brown Apr 14 '15 at 23:50
  • @MikeBrown Try this: ssh -f mike@mycompter_$1.com 'cd/home/temp && nohup Rscript L_'$1'.R> L_'$1'_sh.txt &'. Put it in a file, make the file executable (chmod +x filename), then each time you run it, pass in the number of the computer, ex, ./commandfile 2 will execute it on machine 2, and so on. – Cormac Apr 14 '15 at 23:52
  • @MikeBrown chmod +x filename marks filename as an executable file. After you do this, you can execute the file from anywhere by just typing /path/to/filename at the terminal, or if you're in the same directory as filename, you can use ./filename. Look at this page for more details on this. – Cormac Apr 15 '15 at 0:12
0

SSH can be invoked in a few ways. The most common way people invoke it is to use:

ssh user@host

This opens an interactive terminal. However, SSH behind the scenes actually has a calling style more like this:

ssh user@host /usr/bin/foo

So the first type basically just connecting and running bash for you, it's a convenience. It's basically doing this for you:

ssh user@host sh

Where sh is the shell for that system, which is probably bash.

For the sake of argument let's assume your command you need to run is mycommand.

(The reason I am simplifying this is because the command you gave as an example contains nohup and output redirection, meaning you need to properly escape the command. Also, if you're using nohup in an effort to make the command stay alive after the SSH connection terminates, you probably want to use setsid to make it a session leader. In either case you're going to also make it an asynchronous command using &)

For different computers, I need to send them different command.

You didn't specify how you want to do this. For example, is the machine initiating the SSH connection the one that decides which command is ran, or is it specified by a configuration on the remote machine?

If your machine initiating the connections to various remote machines is the one that should decide which command is written, you need to store that information somewhere and use it.

One way you can do this would be to pipe data into SSH, since by default it will invoke bash, which will read from standard input:

cat command.txt | ssh user@host

I've used cat comand.txt as an example here that generates which command should be sent to SSH, because I have no idea how you are saving that information right now. It may be easiest to store all the information in a text file like this:

user  host  command
user2 host2 command2
user3 host3 command3

You could now create a pipeline that does it, although it will be somewhat complicated (you'll have to use a parser like sed or awk). It may be easier to use a light scripting language like Perl or PHP:

<?php

foreach (file('myfile.txt') as $line) {
    list($user, $host, $command) = explode(' ', $line);
    passthru("ssh $user@$host $command");
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.