How to correctly handle autorun start & stop on linux with python -


i have 2 scripts: "autorun.py" , "main.py". added "autorun.py" service autorun in linux system. works perfectly!

now question is: when want launch "main.py" autorun script, , "main.py" run forever, "autorun.py" never terminates well! when do

sudo service autorun-test start 

the command never finishes!

how can run "main.py" , exit, , finish up, how can stop "main.py" when "autorun.py" launched parameter "stop" ? (this how other services work think)

edit:

solution:

if sys.argv[1] == "start":     print "starting..."     daemon.daemoncontext(working_directory="/home/pi/python"):         execfile("main.py") else:     pid = int(open("/home/pi/python/main.pid").read())     try:         os.kill(pid, 9)         print "stopped!"     except:         print "no process pid "+str(pid) 

first, if you're trying create system daemon, want follow pep 3143, , want use daemon module you.

when want launch "main.py" autorun script, , "main.py" run forever, "autorun.py" never terminates well!

you didn't how you're running it. if you're doing launches main.py child , waits (or, worse, tries import/execfile/etc. in same process), can't that. either autorun.py has launch , detach main.py (or indirectly via external tool), or main.py has daemonize when launched.

how can stop "main.py" when "autorun.py" launched parameter "stop" ?

you need form of inter-process communication (ipc), , way autorun find right ipc channel use.

if you're building network server, right answer might connect client. otherwise, simplest thing kill process signal.

if you're using daemon module, can map signals callbacks. or, if don't need cleanup, use sigterm, default abruptly terminate. if neither of applies, have set custom signal handler (and within handler useful—e.g., set flag main code checks periodically).

how know process send signal to? standard way have main.py record pid in pidfile @ startup. read pidfile, , signal whatever process specified there. (if error because there no process pid, means daemon quit reason—possibly because of unhandled exception, or segfault. may want log that, treat "stop" successful otherwise.) again, if you're using daemon, pidfile stuff you; if not, have yourself.

you may want take @ service scripts daemons came computer. they're written in bash rather python, it's not hard figure out they're doing. or… use 1 of them skeleton, in case don't need any bash knowledge; it's search-and-replace on name.

if distro has lsb-style init functions, can use this example. 1 whole lot more need to, it's example of of details. or scratch this example. 1 doing pidfile management , backgrounding service script (turning non-daemon program daemon), don't need if you're using daemon properly, , it's using sighup instead of sigterm. can google other examples of init.d service scripts.

but again, if you're trying own system, best thing inside /etc/init.d on distro. there dozens of examples there, , 90% of them same except name of daemon.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -