Subprocess check_output returned non-zero exit status 1

The command yum that you launch was executed properly. It returns a non zero status which means that an error occured during the processing of the command. You probably want to add some argument to your yum command to fix that. Your code could show this error this way: import subprocess try: subprocess.check_output(“dir /f”,shell=True,stderr=subprocess.STDOUT) except … Read more

‘^’ is ignored by Python – how to escape ‘^’ character in Popen Windows?

Why Python cuts ‘^’ character and how to avoid it? Python does not cut ^ character. Popen() passes the string (resize_command) to CreateProcess() Windows API call as is. It is easy to test: #!/usr/bin/env python import sys import subprocess subprocess.check_call([sys.executable, ‘-c’, ‘import sys; print(sys.argv)’] + [‘^’, ‘<– see, it is still here’]) The latter command … Read more

Sending a password over SSH or SCP with subprocess.Popen

Here’s a function to ssh with a password using pexpect: import pexpect import tempfile def ssh(host, cmd, user, password, timeout=30, bg_run=False): “””SSH’es to a host using the supplied credentials and executes a command. Throws an exception if the command doesn’t return 0. bgrun: run command in the background””” fname = tempfile.mktemp() fout = open(fname, ‘w’) … Read more

Subprocess timeout failure

check_output() with timeout is essentially: with Popen(*popenargs, stdout=PIPE, **kwargs) as process: try: output, unused_err = process.communicate(inputdata, timeout=timeout) except TimeoutExpired: process.kill() output, unused_err = process.communicate() raise TimeoutExpired(process.args, timeout, output=output) There are two issues: [the second] .communicate() may wait for descendant processes, not just for the immediate child, see Python subprocess .check_call vs .check_output process.kill() might not … Read more

Python: subprocess call with shell=False not working

You need to split the commands into separate strings: subprocess.call([“./rvm”, “xyz”], shell=False) A string will work when shell=True but you need a list of args when shell=False The shlex module is useful more so for more complicated commands and dealing with input but good to learn about: import shlex cmd = “python foo.py” subprocess.call(shlex.split(cmd), shell=False) … Read more

Interacting with bash from python

Try with this example: import subprocess proc = subprocess.Popen([‘/bin/bash’], stdin=subprocess.PIPE, stdout=subprocess.PIPE) stdout = proc.communicate(‘ls -lash’) print stdout You have to read more about stdin, stdout and stderr. This looks like good lecture: http://www.doughellmann.com/PyMOTW/subprocess/ EDIT: Another example: >>> process = subprocess.Popen([‘/bin/bash’], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE) >>> process.stdin.write(‘echo it works!\n’) >>> process.stdout.readline() ‘it works!\n’ >>> process.stdin.write(‘date\n’) >>> process.stdout.readline() … Read more

What is the subprocess.Popen max length of the args parameter?

If you’re passing shell=False, then Cmd.exe does not come into play. On windows, subprocess will use the CreateProcess function from Win32 API to create the new process. The documentation for this function states that the second argument (which is build by subprocess.list2cmdline) has a max length of 32,768 characters, including the Unicode terminating null character. … Read more

Advantages of subprocess over os.system

First of all, you are cutting out the middleman; subprocess.call by default avoids spawning a shell that examines your command, and directly spawns the requested process. This is important because, besides the efficiency side of the matter, you don’t have much control over the default shell behavior, and it actually typically works against you regarding … Read more