3

I'm trying to create a shell script which will execute some commands which I require frequently. I made the ssh login to skip the password prompt using a public/private key pair. and after some search, I'm able to execute some codes like:

File: ssh.sh

ssh -X root@172.16.0.1 << EOF
cd /root/myDir
gedit a.c b.c
EOF

But I don't see any difference if I put like:

ssh -X root@172.16.0.1 << EOF
cd /root/myDir
gedit a.c b.c &
EOF

The & doesn't make any difference.With &, generally after executing gedit, it'll be sent to background and prompt will come. But I'm not getting the prompt after gedit. (Though whatever there in the next line is getting executed. Like if I put echo "Hello", it's displaying Hello.)

What's wrong here? Is there any other method? I want to do a SSH and execute some command through shell script. But I don't want to logout after the shell script finishes its execution. I want to logout manually after executing some of my own commands which are not repetitive like the above script.

  • What did you expect to happen, and what happened instead? – lcd047 May 19 '15 at 8:52
  • I expected with & I'll get the prompt by sending the gedit to background. But I'm not getting the prompt back. – RatDon May 19 '15 at 8:55
  • That's because ssh gets EOF on stdin. The connection is still open because gedit is still running, but the shell is gone. – lcd047 May 19 '15 at 9:02
  • @lcd047 Updated the question what I want and expect. Without EOF, I'm unable to open gedit remotely. As ssh gets executed, then when i logout, the rest part is executed locally. is there any other alternative? – RatDon May 19 '15 at 9:02
  • 1
    Then I suggest to run tmux on the server, and use a text-mode editor. You can even detach tmux from the terminal and leave it running after you quit ssh, and re-attach to it later. – lcd047 May 19 '15 at 9:34
3

Without &, the script that you're executing remotely says “change to /root/myDir, then execute gedit and wait for it to exit”. The variant with & says not to wait for gedit to exit. Either way, the shell exits once it's executed the last command in the script.

If you want to execute a script and then execute more commands that you type interactively, you need to execute an interactive shell at the end.

ssh -Xt root@172.16.0.1 'cd /root/myDir && gedit a.c b.c; exec bash'

The option -t tells SSH to set up a virtual terminal on the server; by default it doesn't do that when you pass a remote command to execute. exec bash at the end tells the shell to replace itself by a new instance of bash, which will be an interactive shell (showing a prompt, listening to your commands, etc.) since its input is coming from a terminal.

0

You can define a function and then execute that over ssh.

#!/bin/bash
ssh_func(){

    cd /root/myDir
    gedit a.c b.c
}

ssh -X root@172.16.0.1 "$(typeset -f); ssh_func"

typeset -f will display the functions defined within the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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