Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In my script I do something like this:

command="some/path/script.sh arg1 arg2; some/path/script2.sh arg1 arg2;"
ssh_command="ssh root@$ip '$command'"

echo $ssh_command
exec $ssh_command

The echo gives output like this:

ssh root@1.1.1.1 'some/path/script.sh arg1 arg2; some/path/script2.sh arg1 arg2;'

After the "exec" thing I get the output:

bash: some/path/script.sh arg1 arg2; some/path/script2.sh arg1 arg2;: No such file or directory

However, when copy the command from echo output and run it directly from the terminal it works like charm. Any ideas why?

share|improve this question

2 Answers 2

exec replaces the shell with a program, invoking it with the arguments supplied. The shell sees 2 tokens: "exec", and "some/path/script.sh arg1 arg2; some/path/script2.sh arg1 arg2;". The second argument is interpreted as a path to a program to execute, including all the spaces and semicolons. Only a shell knows to split arguments on whitespace and to separate commands at a semicolon. Therefore, you should replace the exec with a call to a shell, like sh -c "$ssh_command".

share|improve this answer
    
Or, do what you probably really meant to: eval "$ssh_command". The quotes might not be necessary. If you want this to end the script, use eval exec $ssh_command. –  Scott Feb 6 '13 at 18:26

Nohup may be useful to you.

Try integrating something like this from the link below.

ssh -n -f user@host "sh -c 'cd /whereever; nohup ./whatever > /dev/null 2>&1 &'"

http://stackoverflow.com/q/29142/1666510

share|improve this answer
1  
nohup is a useful tool, but the part of your command that actually solves the problem is the "sh -c", not nohup. –  bonsaiviking Feb 6 '13 at 15:27
    
Thanks for the clarification. –  Enigma Feb 6 '13 at 15:28

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.