Python Forum
win10 Service(script) start and stop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
win10 Service(script) start and stop
#1
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!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to stop while loop from another script Absolutewind 5 819 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  ''.join and start:stop:step notation for lists ringgeest11 2 2,381 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  non-stop ping script kucingkembar 1 1,321 Aug-23-2022, 06:29 AM
Last Post: menator01
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,476 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  Script stop work after 3 actioins - PLEASE WHERE IS THE PROBLEM? rondon442 0 1,533 Sep-27-2021, 05:40 PM
Last Post: rondon442
  Where does pip auto install on Win10 with py 3.9.4? Fran_3 4 3,454 May-05-2021, 03:47 PM
Last Post: snippsat
  Compiling on win10 ebolisa 1 1,725 Sep-04-2020, 04:39 PM
Last Post: ebolisa
  Looking for help on making a script [no idea where to start] Chietnemese 1 1,708 Jun-26-2020, 03:50 AM
Last Post: Larz60+
  How can I stop the script when I run the game on my own computer? StatTark 2 2,193 Jun-19-2020, 11:20 AM
Last Post: StatTark
  Help for Windows service recycling script Rajesh_K 1 1,791 Apr-22-2020, 06:07 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020