I want a particular function to run in background using a child process and I would also like to terminate the child process when needed.
I am storing the pid of the child process in a file. So, when user types something like "python script.py exit", I can accept the command line arguement and kill the child.
import os
import time
import notify2
f = open("output.txt","w")
def notify(message):
notify2.init("Cricket Matches")
n = notify2.Notification("Live Update",message)
n.show()
def fun():
while(True):
notify("match")
pid = os.fork()
if pid == 0:
fun()
else:
#do other stuff
f1.write(str(pid))
Are there better alternatives to achieve the same? I have found nohup and screen tough to implement from the script(not from the command line).