You cannot return to the same terminal with plain ssh (the pty gets destroyed as soon as ssh disconnects), and in fact there is no terminal when you run commands in "batch mode" – just raw stdout. (There's -t
to force a terminal though.)
However, you can do this with terminal multiplexers like tmux
, screen
, or dtach
, which are mostly written for this exact purpose:
ssh myserver "tmux new-session -d -n myscript ./myScript.py" ssh -t myserver "tmux attach -n myscript" ssh myserver "tmux kill-session -t myscript"
With dtach:
ssh myserver "dtach -n ~/myscript.sock ./myScript.py" ssh -t myserver "dtach -a ~/myscript.sock"
With screen:
ssh myserver "screen -dmS myscript ./myScript.py" ssh -t myserver "screen -r myscript"