[clug-talk] running a remote process
carlsonmark at gmail.com
Wed Apr 8 10:24:20 PDT 2009
On 4/8/09, Mark Carlson <carlsonmark at gmail.com> wrote:
> On 4/8/09, Greg Saunders <greg at taord.com> wrote:
> > Hi all, I'm looking for the best way to run a remote process. SSH with keys?
> > "ssh user at host.domain.com remoteprocess"
> > Is there a better way, especially if you have to execute 20 remote commands
> > each time and those commands are determined on the fly and not known ahead
> > of time.
> > Thanks!
> > Greg
> "best" really depends on your situation...
> Personally, I find SSH to be the best for me. If you need to use
> keys, use them, if you don't want to, don't.
> If you want to execute 20 commands, use a list of commands.
> Some simple examples:
> # Commands are executed sequentially:
> % command1; cmd2; cmd3
> # Commands are only executed if the previous one returns zero:
> % command1 && cmd2 && cmd3
> # Commands are only executed if the previous one returned nonzero (error):
> % command1 || cmd2 || cmd3
> A more complex example:
> # Run command 1, then command 2, and finally command 3 only if command
> 2 did not return an error
> % command1; cmd2 && cmd3
> From the bash man page: http://linux.die.net/man/1/bash
> Shell Grammar
> A list is a sequence of one or more pipelines separated by one of the
> operators ;, &, &&, or ||, and optionally terminated by one of ;, &,
> or <newline>.
> Of these list operators, && and || have equal precedence, followed by
> ; and &, which have equal precedence.
> A sequence of one or more newlines may appear in a list instead of a
> semicolon to delimit commands.
> If a command is terminated by the control operator &, the shell
> executes the command in the background in a subshell. The shell does
> not wait for the command to finish, and the return status is 0.
> Commands separated by a ; are executed sequentially; the shell waits
> for each command to terminate in turn. The return status is the exit
> status of the last command executed.
> The control operators && and || denote AND lists and OR lists,
> respectively. An AND list has the form
> command1 && command2
> command2 is executed if, and only if, command1 returns an exit status of zero.
> An OR list has the form
> command1 || command2
> command2 is executed if and only if command1 returns a non-zero exit
> status. The return status of AND and OR lists is the exit status of
> the last command executed in the list.
> -Mark C.
Oh, and if you don't use quotes to enclose the commands, you may be
surprised by the results!
Ex: both commands run on remote machine:
% ssh 127.0.0.1 "echo hello; echo world"
Ex: "echo hello" run remotely, "echo world" run locally
% ssh 127.0.0.1 echo hello; echo world
The results look the same in this case... but if your commands are
backing up a computer or something, you will soon notice a difference!
More information about the clug-talk