Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subprocess check_output returned non-zero exit status 1

This is my python code:

import subprocess subprocess.check_output("ls",shell=True,stderr=subprocess.STDOUT)  import subprocess subprocess.check_output("yum",shell=True,stderr=subprocess.STDOUT) 

The first .check_output() works well, but the second returns this:

Traceback (most recent call last): File "/usr/lib/x86_64-linux-gnu/gedit/plugins/pythonconsole/console.py", line 378, in __run r = eval(command, self.namespace, self.namespace) File "<string>", line 1, in <module> File "/usr/lib/python3.4/subprocess.py", line 616, in check_output raise CalledProcessError(retcode, process.args, output=output) subprocess.CalledProcessError: Command 'yum' returned non-zero exit status 1 

Why does this happen? Is it because ls is the original shell command but yum is the new package? How can I solve this problem?

like image 939
Zongze Wu Avatar asked Jan 13 '15 11:01

Zongze Wu


People also ask

What is subprocess Check_output in Python?

The subprocess. check_output() is used to get the output of the calling program in python. It has 5 arguments; args, stdin, stderr, shell, universal_newlines. The args argument holds the commands that are to be passed as a string.

What is a non-zero exit code?

A non-zero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command terminates on a fatal signal whose number is N , Bash uses the value 128+ N as the exit status.

What is the return type of subprocess Check_output?

CalledProcessError Exception raised when a process run by check_call() or check_output() returns a non-zero exit status. returncode Exit status of the child process.

How do you call a subprocess in Python?

To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.


2 Answers

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 subprocess.CalledProcessError as e:     raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output)) 
like image 182
Plouff Avatar answered Sep 17 '22 15:09

Plouff


The word check_ in the name means that if the command (the shell in this case that returns the exit status of the last command (yum in this case)) returns non-zero status then it raises CalledProcessError exception. It is by design. If the command that you want to run may return non-zero status on success then either catch this exception or don't use check_ methods. You could use subprocess.call in your case because you are ignoring the captured output, e.g.:

import subprocess  rc = subprocess.call(['grep', 'pattern', 'file'],                      stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) if rc == 0: # found    ... elif rc == 1: # not found    ... elif rc > 1: # error    ... 

You don't need shell=True to run the commands from your question.

like image 26
jfs Avatar answered Sep 18 '22 15:09

jfs