[SOLVED]Run Command/Shell Script from within Python Script

Posted on
Tue Jun 10, 2014 4:47 pm
cpeek offline
Posts: 15
Joined: Jun 19, 2013

[SOLVED]Run Command/Shell Script from within Python Script

Is there a way to execute a command or shell script from within a Python script action that is running as part of a schedule?

For example, I would like to call a UNIX command such as /usr/bin/caffeinate while I am running an Action type of "Execute Script" that is running an embedded Python script.

Thanks.

Posted on
Tue Jun 10, 2014 5:15 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Run Command/Shell Script from within Python Script Actio

The subprocess Python library is the one you want to look at. Just make sure that any command you're calling will return in less than about 10 seconds. Embedded Python scripts run in a shared execution environment and any script that takes longer than 10 seconds is killed so as to not slow other executions. If it takes longer, just execute it as a separate file so it'll get its own process and can run as long as you want.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Nov 08, 2015 6:29 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Run Command/Shell Script from within Python Script Actio

jay (support) wrote:
The subprocess Python library is the one you want to look at. Just make sure that any command you're calling will return in less than about 10 seconds. Embedded Python scripts run in a shared execution environment and any script that takes longer than 10 seconds is killed so as to not slow other executions. If it takes longer, just execute it as a separate file so it'll get its own process and can run as long as you want.

Is there a way to get the subprocess() return back to Indigo? I had a dickens of a time chasing down a bug because everything seemed to be running okay, (Indigo just couldn't see the error return.) I was thinking that it might be possible to have the subshell log any errors to a file and then have the calling Indigo process look to see if there are any errors in that file, but that seems kludgy.

The environment I'm talking about:
Indigo calls external_script.py ---> external_script.py calls subprocess(another_script.py, shell=True) --> another_script.py throws an error

Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Nov 08, 2015 10:25 am
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: [SOLVED]Run Command/Shell Script from within Python Scri

Is there a way to get the subprocess() return back to Indigo?

Dave, I think that you want to start the processs with subprocess.Popen(); get the return and call the .communicate() method which returns the standard out and standard error streams; look up the documentation on this first as I haven't used it but had investigated it a while back when I thought that I would need it.

Adam

Posted on
Sun Nov 08, 2015 1:17 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: [SOLVED]Run Command/Shell Script from within Python Scri

What I do in the run shell script action is to redirect stdout to a pipe and save the process id into a list of active scripts. Then, in runConcurrentThread I cycle through the list looking at the script processes returncode property. If it's not None then I know it's done. If it's non-zero, then the script returned an error. If it's 0, then I read standard out (processVar.stdout.read()) to get all the output and stuff it into a variable.

I think there are a variety of ways of doing it in Python, but I needed a non-blocking way so I used a pipe.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Nov 08, 2015 4:12 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: [SOLVED]Run Command/Shell Script from within Python Scri

Thanks guys, I'll have a look. You've given some good stuff to chew on tonight. :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Nov 08, 2015 9:47 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: [SOLVED]Run Command/Shell Script from within Python Scri

Update: I'm pretty much in business with this.

The following gets me what I need:
Code: Select all
command = process_I_want_to_run
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = proc.communicate()
indigo.server.log(u"command error: %s" % err)

I introduced an error into the command subprocess and it was properly written to the Indigo log. There's more I can do with respect to Jay's suggestion, but this gets me in the neighborhood while I monkey around. In the irony department, I had the bulk of this in other code based on a tip from @raneil--for a completely different reason. No matter--thanks for the help guys!

Dave

Edit: to correct syntax errror of last code line.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests

cron