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.

  • As an aside, screen sessions belong to a user, so you can't have "other people ... to be able to access it". If it launches at startup the screen session will belong to root, not you. – roaima Oct 20 '15 at 11:16

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`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.