I have the following Python(2.7) code:
try:
FNULL = open(os.devnull,'w')
subprocess.check_call(["tar", "-czvf", '/folder/archive.tar.gz', '/folder/some_other_folder'], stdout=FNULL, stderr=subprocess.STDOUT)
except Exception as e:
print str(e)
The problem which I face is that, when there is no more space for the archive, print str(e) prints Command '['tar', '-czvf', '/folder/archive.tar.gz', '/folder/some_other_folder']' returned non-zero exit status 1, which is true, but I want to catch the real error here, that is gzip: write error: No space left on device (I got the this error when I ran the same tar comand manually). Is that possible somehow? I assume that gzip is another process within tar. Am I wrong? Please keep in mind that upgrading to Python 3 is not possible.
EDIT: I also tried to use subprocess.check_output() and print the contents of e.output but that also didn't work
if tar -czvf folder.archive.gz /folder; then ...; else echo "There was an error" >&2; fiin shell, it's the exit status that the shell is relying on to know that something went wrong and follow theelsebranch.stderrwhen the exit status is non-zero, so sure, you might see diagnostic output when an error occurs, but it'll be silent under normal (success) conditions. Definitely not relying on whetherstderris empty/non-empty.