Executing a system command within python

GOAL: determine if a specific task is running on another machine using Ignition
if the task is NOT running, notify via alerts a user (in my case, a text to my phone)

I have a few SQL tags configured to facilitate this journey,
Computer name (or IP address),
TaskName to query (using the systems ‘tasklist.exe’ command)
username,
password

Within a script module I have the following code…

def Chk4Task(computer, username, password, taskname):
	import app
	
	cmd = 'tasklist.exe /s ' + computer + ' /u ' + username + ' /p ' + password # + ' /fi "Imagename eq ' + taskname + '"'
	
#	print cmd 
	out = app.Util.execute(cmd)
#	print out
	success = 0
	
#	print out[0]

	for row in out[0]:
		if row.rfind(taskname) != -1:
			success = 1
	
#	print "success = %d" % success
	return success

Within a button’s event (mouse pressed) I have been successful in executing this new routine,
HOWEVER…
when I execute the SAME code from within a Gateway Script (say every 30 minutes) the code fails. when I print out[0] (from above) its a null list.

I don’t think showing the execute() script since it works on a ‘button’…maybe I’m wrong…

Any thoughts?

Is the client or designer you are pressing the button in running on the same computer as the Ignition Gateway?

You can check the wrapper.log file on the computer running the Gateway Script to see if any exceptions are being thrown which can contain useful data that tells you what is happening.

Here’s some documentation about debugging Gateway Scripts, including where to find the wrapper.log file:
inductiveautomation.com/supp … script.htm

Actually I use the wrapper.log file for debugging and when I print out the results from the execute command, nothing appears, when running from a Gateway Script. I get valid results when from a client app however… what a puzzle…

Hi dadof3and3,

You didn’t answer my question. Is the client/designer running on the same machine as your Gateway Scripts?

I’m not sure what you are saying in your last post. The wrapper.log file can only be used to debug Gateway Scripts and cannot be used to debug scripts on a button. If you put prints statements, for example “print ‘hello’”, in your Gateway Script are they showing up in the wrapper.log file?

If you post or send me your wrapper.log file I will take a look at it and see if I can see anything if you want.

Could this be a Windows permission problem based on the permissions of the user (button in client) vs the permissions of the gateway service (when using gateway timer script)? Maybe you need to “Allow service to interact with desktop”?

[quote=“nmudge”]Hi dadof3and3,

You didn’t answer my question. Is the client/designer running on the same machine as your Gateway Scripts?

I’m not sure what you are saying in your last post. The wrapper.log file can only be used to debug Gateway Scripts and cannot be used to debug scripts on a button. If you put prints statements, for example “print ‘hello’”, in your Gateway Script are they showing up in the wrapper.log file?

If you post or send me your wrapper.log file I will take a look at it and see if I can see anything if you want.[/quote]

The client is running on my desktop while the gateway is a server in another area. I do use the wrapper.log file to debug gateway scripts. I use the console to debug local client scripts.

Permissions did cross my mind... The tasklist.exe Windows command requires a user/password command line options to get the task list from other machines. When I remote into the server (gateway) the command works as advertised yet the same command doesn't work via Gateway Scripts.

(BTW: Thanks for all the responses..)

When you remote into the server, you have to provide login credentials. The Ignition Gateway is a service that runs on the computer (by default under the Local System Account). You can change to a specific user in the services menu and see if that fixes it.