close

[Solved] Python ‘subprocess’ CalledProcess: Command ‘[…]’ returned non-zero exit status 1

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python ‘subprocess’ CalledProcess: Command ‘[…]’ returned non-zero exit status 1 in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python ‘subprocess’ CalledProcess: Command ‘[…]’ returned non-zero exit status 1 Error Occurs?

Today I get the following error Python ‘subprocess’ CalledProcess: Command ‘[…]’ returned non-zero exit status 1 in python.

How To Solve Python ‘subprocess’ CalledProcess: Command ‘[…]’ returned non-zero exit status 1 Error ?

  1. How To Solve Python 'subprocess' CalledProcess: Command '[…]' returned non-zero exit status 1 Error ?

    To Solve Python 'subprocess' CalledProcess: Command '[…]' returned non-zero exit status 1 Error As your error message said, ping finished with non zero exit status. It might mean that e.g. the IP address provided is not reachable or you passed in wrong parameters.

  2. Python 'subprocess' CalledProcess: Command '[…]' returned non-zero exit status 1

    To Solve Python 'subprocess' CalledProcess: Command '[…]' returned non-zero exit status 1 Error As your error message said, ping finished with non zero exit status. It might mean that e.g. the IP address provided is not reachable or you passed in wrong parameters.

Solution 1

subprocess.check_output raises CalledProcessError on non-zero exit code, and ping returns non-zero exit code if something is wrong (e.g. unknown domain name, or site is down, or site has ICMP blocked for some reason, or your Internet connection is down).

If you want to examine both output and exit code, use subprocess.Popen:

import subprocess
import sys

site = sys.argv[1]
ping_count = 4
process = subprocess.Popen(['ping', site, '-c', str(ping_count)],
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT)
returncode = process.wait()
print('ping returned {0}'.format(returncode))
print(process.stdout.read())

Examples:

$ python ping.py google.com         <-- ping successful
ping returned 0
PING google.com (195.64.213.27) 56(84) bytes of data.
64 bytes from cache.google.com (195.64.213.27): icmp_seq=1 ttl=57 time=59.8 ms
64 bytes from cache.google.com (195.64.213.27): icmp_seq=2 ttl=57 time=2.43 ms
64 bytes from cache.google.com (195.64.213.27): icmp_seq=3 ttl=57 time=77.0 ms
64 bytes from cache.google.com (195.64.213.27): icmp_seq=4 ttl=57 time=43.8 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 2.439/45.802/77.096/27.658 ms

$ python ping.py asdasdas.com       <-- DNS resolved, but site is down
ping returned 1
PING asdasdas.com (69.172.201.208) 56(84) bytes of data.

--- asdasdas.com ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3024ms

$ python ping.py asdasdasdasda.com  <-- DNS failed
ping returned 2
ping: unknown host asdasdasdasda.com

Solution 2

As your error message said, ping finished with non zero exit status. It might mean that e.g. the IP address provided is not reachable or you passed in wrong parameters.

From ping man page (http://linux.die.net/man/8/ping):

If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read