This might be in the "workaround" category, but I was able to achieve what I think your goal is by putting the command ("python dostuff.py" from your example) into a shell script, then running ... screen -d -m "/path/to/that/script"
Version info:
$ screen -v
Screen version 3.09.10 (FAU) 4-Sep-01
Looks like I have a pretty old version of screen on this system! Perhaps screen's behavior changed in more recent versions to automatically exit?
$ screen -ls
No Sockets found in /tmp/uscreens/S-username.
$ screen -dm "ls"
$ screen -ls
There is a screen on:
32112694.ls (Detached)
1 Socket in /tmp/uscreens/S-username.
(screen -r; exit)
$ screen -ls
No Sockets found in /tmp/uscreens/S-username.
$ cat runme
#!/bin/sh
sleep 6
ls > now
$ screen -d -m ./runme
$ ps -ef|grep sleep
username 9633926 9437204 0 14:55:23 pts/1 0:00 grep sleep
username 15532242 10223670 0 14:55:21 pts/6 0:00 sleep 6
(wait 6 seconds)
$ screen -ls
No Sockets found in /tmp/uscreens/S-username.
$ ls now
now
One thing I realized when going through this was that my version of screen seemed to expect a session name after -dm
; I had to separate -d
and -m
in order to pass a command argument.
Try using:
ssh user@host screen -d -m python dostuff.py
with an optional -S
to name the session something specific.
screen -dm "ls;exit"
– Jeff Schaller Mar 01 '16 at 17:43screen -S foo -dm "sleep 10"
followed byscreen -ls
will show a screen namedfoo
for ten seconds; it then disappears. Doing the same to invoke a python script that usestime.sleep(10)
behaves the same- the screen is terminated when the script is completed. This is with Screen version 4.00.03 (FAU). – DopeGhoti Mar 01 '16 at 18:22