0

I have this function within a Python script that has been installed as a windows service with NSSM. When the Python script is run normally (i.e. not as a service), the file is printed. However, when it is installed as a service it does not print.

Print is externally triggered over opc-ua.

Is it even possible to print while the script is installed as a service? While installed as a service, I don't get any exceptions either. Sometimes I got KeyboardInterrupt exception while sleeping between loops.

def CheckOpcuaNode(latestPDF):
    client = Client("opc.tcp://192.168.202.90:4840/")
    try:
        client.connect()
        opcuaNode = client.get_node("ns=6;s=::AsGlobalPV:g_saveParameters.bPrintNow")
        Result = opcuaNode.get_value() 
        if Result == True:
             print("print file: %s" % str(latestPDF))
             os.startfile(latestPDF, "print")
             try:
                time.sleep(4)
             except KeyboardInterrupt:  # Ignore keyborditerruption
                print("ERROR KeyboardInterrupt while printing: %s" % sys.exc_info()[0])
             opcuaNode.set_attribute(ua.AttributeIds.Value, ua.DataValue(False))
    except:
            print("ERROR while checking if PDF shall be printed: %s" % sys.exc_info()[0])
    finally:
        client.disconnect()

def LatestPdf():
    return 'path\to\PDF\file.pdf'

if __name__ == '__main__':
while True:
    latestPdfFile = LatestPdf()
    if latestPdfFile != '':
        CheckOpcuaNode(latestPdfFile) # check If PLC has asked to print pdf file
    try:
        time.sleep(4)
    except KeyboardInterrupt:  # Ignore keyborditerruption
        print("ERROR KeyboardInterrupt between loops: %s" % sys.exc_info()[0])
tomatoeshift
  • 465
  • 7
  • 23
  • Is service the only choice? How about task scheduler? – Boying May 06 '20 at 14:07
  • well, the print command comes externally from another device over OPC-UA. So the wait time would be quite long. From my understanding, task scheduler can only run every 5 minutes? Or is it possible to run task scheduler on a second based time intervall? [This is one solution](https://superuser.com/a/668074/1100184) – tomatoeshift May 06 '20 at 14:11

0 Answers0