79

I want to run a script remotely. But the system doesn't recognize the path. It complains that "no such file or directory". Am I using it right?

ssh kev@server1 `./test/foo.sh`
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Progress Programmer
  • 7,076
  • 14
  • 49
  • 54
  • 1
    Is the `./test/foo.sh` file located on the local or the remote server? – Finesse Jun 30 '19 at 12:59
  • 3
    Possible duplicate of [How to use SSH to run a shell script on a remote machine?](https://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-a-shell-script-on-a-remote-machine) – DennisLi Jul 11 '19 at 06:04

6 Answers6

189

You can do:

ssh user@host 'bash -s' < /path/script.sh
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
German Attanasio
  • 22,217
  • 7
  • 47
  • 63
  • 1
    Thanks - for me on MediaTemple, separaring the string out like this including the 'bash -s' argument was what i needed. Thanks! – itsricky Jun 23 '13 at 00:57
  • 2
    Just linking how to do this in Perl for reference: http://stackoverflow.com/questions/18236988/execute-perl-script-on-remote-server-from-local-machine – arun Apr 23 '14 at 01:34
  • 2
    Great solution! check out this same solution via `ssh2` module for node: https://gist.github.com/mscdex/7c9f8358b8331ea567b7 – knownasilya May 05 '14 at 13:08
  • 8
    This answer is WAAAY better than the accepted answer and exactly answers the question, whereas the accepted answer does not. – nic Jul 23 '14 at 23:07
  • 6
    Alternatively: `cat /path/script.sh | ssh user@host 'bash -s'` – Claudiu May 06 '15 at 19:28
  • @Claudiu - how does one include arguments for `script.sh` to get passed over ssh? – brucezepplin Jul 13 '15 at 14:07
  • @brucezepplin It's like this: `myparm=foo ; ssh me@remote "mycommand $myparm"`. – MariusMatutiae Jul 28 '15 at 06:36
  • If I try `ssh -t admin@ServerIP 'bash -s' < /var/opt/im/deploy.sh` it cannot find the file. If I to it without `'bash -s' < ` it runs the script but it seems to run locally. Any ideas? I try to mv some folders and restart pm2. Which is executed on the second solution but does not contain any processes. – Andi Giga Feb 18 '16 at 14:54
  • 1
    how to pass arguments to last file? – sites Aug 29 '16 at 00:27
  • In this example, is script.sh on the local computer or the remote computer? – TheJeff Oct 02 '17 at 16:54
  • @TheJeff the script is on the local computer – German Attanasio Oct 02 '17 at 21:50
  • 4
    Example for running with args. Create a sample script: `echo 'printf "%s\n" $(hostname) "$@"' > script` and run it with arguments foo bar baz: `ssh user@host bash -s foo ba{r,z} < script`. – ingydotnet Nov 13 '20 at 19:08
54

Backticks will run the command on the local shell and put the results on the command line. What you're saying is 'execute ./test/foo.sh and then pass the output as if I'd typed it on the commandline here'.

Try the following command, and make sure that thats the path from your home directory on the remote computer to your script.

ssh kev@server1 './test/foo.sh'

Also, the script has to be on the remote computer. What this does is essentially log you into the remote computer with the listed command as your shell. You can't run a local script on a remote computer like this (unless theres some fun trick I don't know).

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
psanf
  • 853
  • 7
  • 4
  • 17
    Here's the fun trick, taken from http://wpkg.org/Executing_local_programs_and_scripts_remotely: cat /usr/bin/program | ssh user@server "cat > /tmp/program ; chmod 755 /tmp/program ; /tmp/program --arguments" – Mark Rushakoff Jun 25 '09 at 00:47
  • 1
    Yea, I figured something like that was possible, but you're not really executing a local program, you're just copying it in a needlessly complex way. If you're going to do that, you could just do scp /path/to/script.sh user@server: && ssh user@server ./script.sh I guess you have to type a password twice this way though, so eh. – psanf Jun 25 '09 at 01:08
  • @psanf You can always setup certificate based login so you don't have to type password. – pm_labs Mar 04 '13 at 11:51
  • Don't do that @MarkRushakoff you execute something in /tmp/program so you expose yourself to a race condition where someone can put file that actually call /tmp/program after it move it. write it in ~. – Et7f3XIV Mar 16 '22 at 23:33
  • @psanf You can use ssh agent – Et7f3XIV Mar 16 '22 at 23:34
24

If you want to execute a local script remotely without saving that script remotely you can do it like this:

cat local_script.sh | ssh user@remotehost 'bash -'

It works like a charm for me.

I do that even from Windows to Linux given that you have MSYS installed on your Windows computer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sergey Kuznetsov
  • 518
  • 4
  • 10
2

I don't know if it's possible to run it just like that.

I usually first copy it with scp and then log in to run it.

scp foo.sh user@host:~
ssh user@host
./foo.sh
Macarse
  • 91,829
  • 44
  • 175
  • 230
1

I was able to invoke a shell script using this command:

ssh ${serverhost} "./sh/checkScript.ksh"

Of course, checkScript.ksh must exist in the $HOME/sh directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit Singh
  • 19
  • 3
0

Make the script executable by the user "Kev" and then remove the try it running through the command sh kev@server1 /test/foo.sh

Milind Jindal
  • 176
  • 1
  • 7