Ssh – Remotely starting a screen session through ssh and closing the ssh session immediately

gnu-screenssh

My goal is to remote to a server through ssh, start a screen, start a script, let the script run, and exit the ssh session while keeping the screen running its own python script. This is what I have:

ssh -t myuser@hostname screen python somepath.py -s 'potato'

The problem with this is, after I run it, I have to manually ctrl + a + d, and exit out of the ssh session myself. Is there a way to do it all in one go without needing human interaction?

EDIT: I have tried the suggested method of using -dm

This is what I'm testing to make it easier to see:

ssh -t user@host screen "top"

remotely I see this:

user      2557  0.0  0.2  27192  1468 ?        Ss   13:35   0:00 SCREEN top
user      2562  0.0  0.1  11740   932 pts/0    S+   13:35   0:00 grep --color=auto SCREEN

but if I do:

ssh -t user@host screen -dm "top"

I immediately get a Connection to host closed. And nothing in my grep

ps aux | grep SCREEN
user      2614  0.0  0.1  11740   932 pts/0    S+   13:36   0:00 grep --color=auto SCREEN

Best Answer

You can use -d -m to your screen session to do it like:

ssh myuser@hostname screen -d -m "python somepath.py -s 'potato'"

That will create a new screen session, run your command in it and automatically detach you from it.

That option is documented as

-d -m
Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.

on the GNU documentation page for screen

Related Question