SSH and Screen – How to Run Command and Detach in One Go

gnu-screenssh

I like to start a server (basically a jupiter-notebook) in a screen session within an SSH, let it run and then detach immediately. doing it one by one:

ssh USER@HOST
screen -d -m /home/USER/anaconda3/bin/jupyter-notebook --ip HOST --port 8889 --no-browser

then detach from screen and finally disconnect from SSH. Then use the server. Everything works in this case. One can also see that the screen is running by

ssh -t USER@HOST screen -ls

Now how do I do the former in one go?

ssh -t USER@HOST "screen -d -m /home/USER/anaconda3/bin/jupyter-notebook --ip HOST --port 8889 --no-browser"

does not work. What am I doing wrong?

thanks

Best Answer

You are quite close, but you don't need the TTY for screen to startup - it creates it's own PTY. But you do need to put the ssh session in the background allowing the screen process to continue running, as in:

ssh -f USER@HOST 'screen -dm command'

Might also see this answer for other things you can do to the running screen session if you name it.

Related Question