Ssh – How to use the “screen” command if I don’t know what the ps or tty the program came from

gnu-screensshtty

I am making a python program that launches on bootup. I would like it so that I can interact with that program via the "screen" command. Then, if I need to talk to the program via a different tty, ssh, or something then I would be able to.

I like to keep it so I can just use a bash script to attach the screen in my terminal, instead of having to look up where it is—as I need other people (who don't use the terminal) to be able to access it.

Best Answer

The surest way to attach to the right screen session is for the Python script to write to a designated file to report which screen session it is running in. Think of it as something analogous to a PID file, except that you are reporting the TTY rather than the process ID. Otherwise, if multiple screen sessions exist, there's going to be guesswork involved.

So, when launching the Python process, run

echo "$STY" > /var/run/python-proc.screen

… or from within the Python program itself,

with open('/var/run/python-proc.screen', 'w') as f:
    print(os.environ['STY'], file=f)

Then, users can attach to it by running

screen -x -m `cat /var/run/python-proc.screen`
Related Question