Python Forum
win10 Service(script) start and stop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: win10 Service(script) start and stop (/thread-11088.html)



win10 Service(script) start and stop - hwa_rang098tkd - Jun-21-2018

I am trying to establish a mqtt client on my pc, So I start writing a script in python. When I run the script from Python IDLE it works fine and stable. I downloaded AlwaysUp and created a service. When I run the service it stops after 5s.
import paho.mqtt.client as mqtt #import the client1
import time
import os
strshtdwn=0
def on_log(clint, userdata,level, buf):
       print("log: "+buf)
def on_connect(client,userdata, flags ,rc):
    if rc==0:
        print("Connected OK")
    else:
        print("Bad connection Returned code=",rc)
def on_disconnect(client, userdata, flags, rc=9):
        print("DisConnected result code "+str(rc))
def on_message(clinet, userdata, msg):
    topic=msg.topic
    m_decode=str(msg.payload.decode("utf-8","ignore"))
    if   m_decode=="SHUTDOWN":
        os.system("shutdown /p")
        client.publish("stat/hwa_rang_pc/POWER","Shuting Down")
    elif m_decode=="RESTART":
        os.system("shutdown /r")
        client.publish("stat/hwa_rang_pc/POWER","Restarting")
    elif m_decode=="LOGOFF":
        os.system("shutdown /l")
        client.publish("stat/hwa_rang_pc/POWER","Logging OFF")
        print("message received: ",m_decode)  
        print("strshtdwn",strshtdwn)
broker="192.168.1.3"
client = mqtt.Client("Hwa_Rang_PC") #create new instance
client.on_connect=on_connect
client.on_disconnect=on_disconnect
#client.on_log=on_log
client.on_message=on_message              
print("Connecting to broker ",broker)
client.connect(broker) #connect to broker
client.loop_start()
client.subscribe("cmnd/hwa_rang_pc/power")
#client.loop_stop()
#client.disconnect() #disconnect
Error by event viewer are 3 at a try . Two with ID "2" and one with ID "7023".
Also I tried with runassvc from pirmasoft and failed: error msg : "the mqtt_commands1 service on local computer started and then stopped. Some services stop aytomatically if the are not in use by other services or programs"
help plz!!!