forked from platypush/platypush
Added support for background shell commands
This commit is contained in:
parent
7f24b82281
commit
165f85f8dc
1 changed files with 5 additions and 1 deletions
|
@ -9,17 +9,21 @@ class ShellPlugin(Plugin):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def exec(self, cmd, ignore_errors=False):
|
def exec(self, cmd, background=False, ignore_errors=False):
|
||||||
"""
|
"""
|
||||||
Execute a command.
|
Execute a command.
|
||||||
|
|
||||||
:param cmd: Command to execute
|
:param cmd: Command to execute
|
||||||
:type cmd: str
|
:type cmd: str
|
||||||
|
|
||||||
|
:param background: If set to True, execute the process in the background, otherwise wait for the process termination and return its output (deafult: False).
|
||||||
:param ignore_errors: If set, then any errors in the command execution will be ignored. Otherwise a RuntimeError will be thrown (default value: False)
|
:param ignore_errors: If set, then any errors in the command execution will be ignored. Otherwise a RuntimeError will be thrown (default value: False)
|
||||||
:returns: A response object where the ``output`` field will contain the command output as a string, and the ``errors`` field will contain whatever was sent to stderr.
|
:returns: A response object where the ``output`` field will contain the command output as a string, and the ``errors`` field will contain whatever was sent to stderr.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if background:
|
||||||
|
subprocess.Popen(cmd, shell=True)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return subprocess.check_output(
|
return subprocess.check_output(
|
||||||
cmd, stderr=subprocess.STDOUT, shell=True).decode('utf-8')
|
cmd, stderr=subprocess.STDOUT, shell=True).decode('utf-8')
|
||||||
|
|
Loading…
Reference in a new issue