I have a long running python script that runs on a remote machine. I'd like to have it check if it is running in a screen session and restart itself in an attached one if not. That way I will still see the output if I am watching it, but it will continue to run if I get disconnected. So far I've tried several variations of
subprocess.run(["screen", "-m", *sys.argv], shell=True)
and
os.execv("/usr/bin/screen", ["screen", python + " " + sys.argv[0]] + sys.argv[1:])
but none of them seem to work.
What is the right way to do this?