I want to SSH into a server, start a screen session, cd
into path/to/my/script/
, and run test.sh
there.
As a starter, I tried
ssh me@myserver.com screen -dm bash -c 'cd path/to/my/script/; pwd > ~/output.txt'
and expected to see path/to/my/script/
in output.txt
, but I see my home directory there. This means the cd
command doesn't really work, so bash won't be able to run test.sh
.
How may I solve this?
Best Answer
The short answer: add some extra quotes around the command, like this:
To see what's going on, you can specify the
-v
option tossh
to obtain some debug information. In this case, you'll see a line like the following for the original commandwhile the extra quotes change this into
So it appears that ssh just takes the arguments that were passed to it, concatenates them all, and lets the remote side split the concatenated argument list again into individual arguments. Calling the argument list
argv
(like in C), you've got something like the following in the original version:Now in principle, it would have been possible for
ssh
to passargv[2]
toargv[6]
as separate arguments to the other side, in which case it would probably have worked as expected. But as the debug line shows (and it also seems like this based on the source code), these arguments are concatenated to the stringwhich is then interpreted at the remote end. From this it's also clear why it doesn't do what you'd like: now you're executing two things in sequence, first
screen -dm bash -c cd path/to/my/script/
(so ascreen
session is started in which only the directory is changed) is executed from the home directory, and thenpwd > ~/output.txt
is executed, also from the home directory.For completeness, the arguments for the command with the double quotes are
causing
screen -dm bash -c 'cd path/to/my/script/; pwd > ~/output.txt'
to be sent to the other side (as shown by the debug line), which does work as intended.