Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh session will not close after running remote command

Tags:

linux

ssh

i have seen similar questions on the web but none of the answers work, hoping a fresh request will reveal an answer.

i am trying to run a remote query on a linux box which i can do the problem is i stay connected to the remote server.

i have set -x in the script so i am watching the output. Part of my script is below

ssh -tt $host.digital.domain.com << EOF
sudo su - foo
/apps/scripts/bar.sh
exit
EOF

the script is runs correctly

[user@server ~]$ [foo@host ~]$ + rm -f '/tmp/files*'

however the window then gives me the prompt on the remote computer instead of exiting.

[foo@host ~]$

i used the -tt switch as i was getting the tty error i have also tried to kill the pid but this doesnt work either. the only way to progress the script is for me to 'ctrl c' to exit the remote session.

i have tried running ssh it all on one line however this didnt work, possibly due to the sudo, i'm not sure.

Any advice would be great.

thanks

like image 452
Red_badger Avatar asked Aug 29 '26 03:08

Red_badger


1 Answers

With the "su" command you open new bash session. If you only write one "exit", you only finish the session of foo user. For finished the session open by the ssh connection you should write other "exit".

ssh -tt $host.digital.domain.com << EOF
sudo su - foo
/apps/scripts/bar.sh
exit  #Exit foo session
exit  #Exit ssh session 
EOF

Output:

sshuser@prompt$  sudo su - foo
foo@prompt$  /apps/scripts/bar.sh
foo@prompt$ exit #Exit foo session
exit
sshuser@prompt$ exit #Exit ssh session
logout
Connection to $host.digital.domain.com closed.
like image 165
fbohorquez Avatar answered Aug 31 '26 01:08

fbohorquez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!