This has been a two day struggle, and I am quite stuck.
TLDR: From within a Python shell (>>> "like this"
), start a Python script that survives quit()
and me@terminal:~$ exit
.
I have tried:
- various daemonizing functions that implement
os.fork()
- creating/calling a shell script with
Popen
and&
appended subprocess.Popen(["nohup", "command.py"])
in several forms
Toy Code - Python 3.6
class MyClass(threading.Thread):
def __init__(self, filepath):
super().__init__()
self.filepath = filepath
def run(self):
import time
i = 0
while True: # Count to 50 in a file forever
if i > 50: i = 0
with open(self.filepath, "a") as afile:
afile.writelines("This is msg {}\n".format(i))
i += 1
time.sleep(1)
How can I run this Thread
from within the Python shell in such a way that it doesn't die when I close my interpreter and close my SSH terminal?
Real Code Django-managed DB consumption of PubNub messages
See # TODO
line, near center. The PNRunner
class instances will be started and killed dynamically within the construct of a Django app, so I can't just start them at boot with a shell script.