Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a server that i can ssh into from my laptop.

I want to be able to run a script on my laptop that sends a signal to the server which makes the server initiate a given script there.

How do I do that?

share|improve this question
2  
Well, just use ssh. What exactly can't you get working? –  Mat Jul 28 '12 at 14:42
    
Using ssh is 3 steps, ssh in, run the script and exit. Can I implement the ssh through a script that would run all 3 commands? –  Hermann Ingjaldsson Jul 28 '12 at 14:44
1  
Try ssh you@yourserver ls and see what it does. –  Mat Jul 28 '12 at 14:45
    
ok, sweet. (fill up space) –  Hermann Ingjaldsson Jul 28 '12 at 14:49
    
Interesting is that in the first ten minutes after posting this question, it received an up vote. But once people see how simple the solution is, they up vote it no more. I state firmly that this question is a valid documentation and worthy of its existence, and the simplicity of its answer only makes it better. Who knows, this documentation may one day help someone. –  Hermann Ingjaldsson Jul 28 '12 at 18:24
add comment

1 Answer

up vote 2 down vote accepted
ssh remotehost 'command1 ; command ; command3'

Or,if you only want to run command2 if command1 succeeded, and command3 if command2 succeeded:

ssh remotehost 'command1 && command && command3'

Note that you'll need to pay special attention to shell quoting issues, they can become very complicated when you're typing a command in your local shell to execute commands on a remote shell (they can get bad enough even just working with a local shell). For example, consider the difference between the following two commands:

ssh remotehost 'ps hu -Csshd | awk "{print \$2}"'
ssh remotehost 'ps hu -Csshd | awk "{print $2}"'

The first prints only the PIDs of sshd processes. The second prints the full ps hu listing of sshd processes - the unescaped $2 is an empty string so the awk command becomes just {print }

A third option is to run the ps on the remote host and the awk locally:

ssh remotehost ps hu -Csshd | awk '{print $2}'

In the two commands above, the entire quoted string is executed on the remote host, because that's the argument you've given to the ssh command. In this example, everything up to the | is executed remotely (again because 'ps', 'hu', and '-Csshd' are args to the ssh command, and the remainder of the line is executed locally.

For tasks that are more complicated than you want to run with a one-liner (or when the shell-quoting becomes hellish), you can write a shell script and copy it to the remote host, then execute it with ssh:

vi myscript.sh
chmod +x myscript.sh
scp myscript.sh remotehost:/usr/local/bin/
ssh remotehost myscript.sh

You can copy the script to anywhere you like, but a) you need write access to the directory, b) it can't be on a filesystem mounted with noexec (not common but sometimes /tmp is mounted noexec) unless you run it with "/path/to/interpreter /path/to/script" - e.g. /bin/bash /tmp/myscript.sh

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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