简体   繁体   中英

Python watchdog application

Erm... i promise i searched thoroughly the web for this and couldn't find a satisfatory answer so >.<

I'm trying to make a 'controller' for my python app, i mean, it should be executed and then spawn the server, wait to pickup exit signals and if necessary, restart the script.

This code is pretty messy but that's what i've come with so far... It always returns with unrecognized exitSignal 1 and won't even spawn the server process! Can someone give me a light please?

#Server controller version 0.1
import os
import time
from datetime import datetime

Log = file("C:/Users/Admin/Desktop/Python/Server/WIP.log", 'w')

def runServer():
    exitSignal = os.spawnv(os.P_WAIT, 'C:/Python2.7/python.exe', ['python.exe',         'C:/Users/Admin/Desktop/Python/Server/WIP.py'])
    print str(datetime.today())+" - Server started"
    Log.write("\n"+str(datetime.today())+" - Server started")

    if exitSignal == "0":
        print str(datetime.today())+" - Server exited succesfully."
        Log.write("\n"+str(datetime.today())+" - Server exited succesfully.")

    elif exitSignal == "10":
        print str(datetime.today())+" - Rebooting server immediately."
        Log.write("\n"+str(datetime.today())+" - Rebooting server immediately.")
        runServer()

    elif exitSignal == "11":
        print str(datetime.today())+" - Rebooting server in 5 minutes."
        Log.write("\n"+str(datetime.today())+" - Rebooting server in 5 minutes.")
        time.sleep(300)
        runServer()
        print str(datetime.today())+" - Server rebooted."
        Log.write("\n"+str(datetime.today())+" - Server rebooted.")

    else:
        print str(datetime.today())+" - Unrecognized exitSignal code: %s" % str(exitSignal)
        Log.write("\n"+str(datetime.today())+" - Unrecognized exitSignal code: %s" % str(exitSignal))

if __name__ == "__main__":
    print str(datetime.today())+" - Controller started."
    Log.write("\n"+str(datetime.today())+" - Controller started")
    runServer()

Sorry for the messy code! (=^,^=)

As described here , os.spawnv is a deprecated method, you should use the subprocess module.

Apart this, if you always got 1 returned and no process spawned, maybe it is due to a bad path to the server code or a bad code for the server part. What happen if you launch the server manually ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM