# System.util.execute with redirected output

**URL:** <https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455>\
**Category:** Ignition\
**Created:** [September 11, 2015, 9:44pm UTC](https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455 "2015-09-11T21:44:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![McAlisterD](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@McAlisterD](https://forum.inductiveautomation.com/u/McAlisterD)\
**Post date:** [September 11, 2015, 9:44pm UTC](https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455/1 "2015-09-11T21:44:48Z")

</div>

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

```auto
"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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 11, 2015, 9:50pm UTC](https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455/2 "2015-09-11T21:50:30Z")

</div>

You probably want the [subprocess](http://www.jython.org/docs/library/subprocess.html) module …

---

<div class="post-metadata">

**Author:** ![McAlisterD](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@McAlisterD](https://forum.inductiveautomation.com/u/McAlisterD)\
**Post date:** [September 14, 2015, 2:40pm UTC](https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455/3 "2015-09-14T14:40:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 14, 2015, 11:22pm UTC](https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455/4 "2015-09-14T23:22:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![McAlisterD](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@McAlisterD](https://forum.inductiveautomation.com/u/McAlisterD)\
**Post date:** [September 15, 2015, 1:59pm UTC](https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455/5 "2015-09-15T13:59:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 15, 2015, 3:14pm UTC](https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455/6 "2015-09-15T15:14:54Z")

</div>

did you “import subprocess” ? works in 7.7.4.

---

<div class="post-metadata">

**Author:** ![CTIJWatts](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@CTIJWatts](https://forum.inductiveautomation.com/u/CTIJWatts)\
**Post date:** [September 18, 2015, 7:01pm UTC](https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455/7 "2015-09-18T19:01:16Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![CTIJWatts](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@CTIJWatts](https://forum.inductiveautomation.com/u/CTIJWatts)\
**Post date:** [September 18, 2015, 7:23pm UTC](https://forum.inductiveautomation.com/t/system-util-execute-with-redirected-output/10455/8 "2015-09-18T19:23:08Z")

</div>

Hey I was able to get it working. I found this post on the forum: [https://inductiveautomation.com/forum/viewtopic.php?p=51323#p51323](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]
