# Getting text output from shell command to log?

**URL:** <https://forum.inductiveautomation.com/t/getting-text-output-from-shell-command-to-log/42167>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [January 12, 2021, 5:48pm UTC](https://forum.inductiveautomation.com/t/getting-text-output-from-shell-command-to-log/42167 "2021-01-12T17:48:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [January 12, 2021, 5:48pm UTC](https://forum.inductiveautomation.com/t/getting-text-output-from-shell-command-to-log/42167/1 "2021-01-12T17:48:46Z")

</div>

I am trying to write a python script that backs up our production database, and logs the result of the backup success/error. I am working on it in the script console. What I have so far is -

```auto
import os
import time
import subprocess
import system.util

logger = system.util.getLogger("Database Backup")

# MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup. 
# To take multiple databases backup, create any file like /backup/dbnames.txt and put databses names one on each line and assignd to DB_NAME variable.
DB_HOST = 'localhost'
DB_PORT = '3306'
DB_USER = 'root'
DB_USER_PASSWORD = 'youwish'
DB_NAME = 'tab'
BACKUP_PATH = 'C:\\Users\\someuser\\Documents\\dumps\\'
MysqldumpPath = 'c:\\Program Files\\MySQL\\MySQL Server 5.6\\bin'  
                

# Getting current datetime to create seprate backup folder like "12012013".
DATETIME = time.strftime('%m%d%Y')

TODAYBACKUPPATH = BACKUP_PATH + DATETIME
logger.info("Todays backup path: " + str(TODAYBACKUPPATH))
FILENAME = "PSM4_"+time.strftime("%H%M%S")+".sql"
# Checking if backup folder already exists or not. If not exists will create it.
if not os.path.exists(TODAYBACKUPPATH):
    logger.info("Creating backup folder")
    os.makedirs(TODAYBACKUPPATH)
FILEPATH = TODAYBACKUPPATH + FILENAME
dumpcommand = "cd C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin & .\\mysqldump --user=%s --password=%s --host=%s --port=%s --all-databases > %s"%(DB_USER,DB_USER_PASSWORD,DB_HOST,DB_PORT,FILEPATH)
print dumpcommand
subprocess.call(dumpcommand, shell=True)
logger.info("Backup script completed. Your backups has been created in '" +TODAYBACKUPPATH + '\\' + db + '.sql'+" directory")

```

For some reason my subprocess.call is not working. I realize I can’t even trouble shoot this as I cannot see what the shell is spitting back out. I would like it if I could get hte output of this shell command so that I could eventually log success or error if one occurs. How can I make that happen with this subprocess library?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 12, 2021, 6:09pm UTC](https://forum.inductiveautomation.com/t/getting-text-output-from-shell-command-to-log/42167/2 "2021-01-12T18:09:51Z")

</div>

Consider using subprocess.Popen() instead.

> <https://stackoverflow.com/questions/2502833/store-output-of-subprocess-popen-call-in-a-string>

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 12, 2021, 6:11pm UTC](https://forum.inductiveautomation.com/t/getting-text-output-from-shell-command-to-log/42167/3 "2021-01-12T18:11:09Z")

</div>

Try using either check\_call or check\_output instead of just call.

Those two functions will raise a CalledProcessError if the return code is non-zero

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [January 12, 2021, 6:19pm UTC](https://forum.inductiveautomation.com/t/getting-text-output-from-shell-command-to-log/42167/4 "2021-01-12T18:19:30Z")

</div>

I was trying that before and kept getting this error

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/9/a9c02788dd32d50d80930b51b117768d6dc45d50.png)

Both folder locations referenced in my dump command, the MySQL and the Users dump folder, both exist, I copied and pasted them from my dump command into a file explorer and was able to see them fine.

I got this error with trying

```auto
dumpcommand = "cd C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin & .\\mysqldump --user=%s --password=%s --host=%s --port=%s --all-databases > %s"%(DB_USER,DB_USER_PASSWORD,DB_HOST,DB_PORT,FILEPATH)
print dumpcommand
p = subprocess.Popen(dumpcommand, stdout=subprocess.PIPE)
result = p.communicate()[0]
print result

```

but it doesn’t get to the print result.

Heres the full dump command -  
`cd C:\Program Files\MySQL\MySQL Server 5.6\bin & .\mysqldump --user=root --password=youwish --host=localhost --port=3307 --all-databases > C:\Users\bkarabinchak\Documents\dumps\01122021\PSM4_131547.sql`

Edit: Fixed this. Needed a `shell=True` parameter.
