System.util.execute with redirected output

I am trying to program a button to back up a database. I need to send the following command to the OS (Win 7).

"C:\Program Files\MySQL\MySQL Server 5.7\bin\MySQLDump.exe"  MySQL > "C:\Users\Public\Documents\Database Backups\Parts.sql"

Can system.util.execute handle redirected output of a command?

Is there a better way to accomplish this?

You probably want the subprocess module …

I am trying to find information about using subprocess. I have found nothing about how to use it in a script. Are there any examples anywhere?

Well, it’s supposed to be the same in jython as in python. I’ve used it myself in a public project: lsdrv. Look at the runx() function starting around line 102.

I tried to use subprocess, but when I run my script it says that subprocess does not exist.

How do I make subprocess available to Ignition?

did you “import subprocess” ? works in 7.7.4.

You are supposed to be able to do this kind of thing with the script function:
system.utils.execute([arg1, arg2, …])
But I have not had much success yet using this.

To use subprocess, you import it first. Take a look at this StackOverflow article for some more information on launching Windows commands through Python using subprocess: http://stackoverflow.com/a/435669/579148

I’ve been doing some testing with this today. Make sure you save before running any commands with subprocess. I locked up my designer completely and had to hard kill the application and lost all my unsaved changes.

Hey I was able to get it working. I found this post on the forum: https://inductiveautomation.com/forum/viewtopic.php?p=51323#p51323

The code I needed is:

from subprocess import Popen, call call(["{command line text here}"], shell=True)

What I was trying to do is open a document that is being stored in the database in it’s default Windows program. So the full code for me is:

[code]from subprocess import Popen, call

#Get the document id
docid = event.source.parent.getComponent(‘ptDocs’).DocID

if docid > 0:
#Get the document data and extension
sql = “SELECT Extension, Data FROM Documents WHERE id = ?”
res = system.db.runPrepQuery(sql, [docid])
ext = res[0][0]
data = res[0][1]

#Get the temp file
path = system.file.getTempFile(ext)

try:
	#Save the file data
	system.file.writeFile(path, data, False) #False=>overwrite
	
	#Open the file
	call([path], shell=True)
		
except Exception:
	system.gui.messageBox("Unable to open the file.", "Open File Error")

[/code]