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.
The print command is externally triggered over opc-ua from another device.
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.
Is there another way to print a file triggered externally over opc-ua?
I know I could for example create 20 different triggers in Windows task scheluler, that thus would be executed every 5 seconds and check the opc-ua variable. This seems like a very tedious process
Here is a section of my Python script:
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])
os.startfile(latestPDF, "print")
opened adobe acrobat and prints the file (at least when the script is executed regularly i.e. not as a service)print to PDF
, so that might be a problem here since it expects input of user to specify a location to save the file. The service is installed so it runs aslocal system account
.