简体   繁体   中英

How many kinds of returncode of subprocess.wait()

I'm so confused about a python-file , which used for copying files from sever to hadoop.

the cmd is: hadoop fs -put /localhost/* /hadoop/* the code is:

    cmd = exc_path + ' ' + 'fs -put' + ' ' + src_path + item + ' ' + dst_path
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    code = process.wait()
    logfile.info('type(code) is %s, code is %s\n', type(code), code)

For the past several days, it runs ok. But the day before yesterday it returned code!=0 Then yesterday, it ran ok, the code == 0 , and then today it fails and returned : type(code) is <type 'int'>, code is 255

The doc says the wait() should return 0 or None, so why do I get a 255? And the cmd 'put' of hadoop should return 0 (when success) and -1 (when fail).

I have found some useful infomation: "Sadly when running your subprocess using shell=True, wait() will only wait for the sh subprocess to finish and not for the command cmd." from

The doc says the wait() should return 0 or None, but why I get a 255.

This is wrong. The documentation says:

Popen.wait() Wait for child process to terminate. Set and return returncode attribute.

And

Popen.returncode The child return code, set by poll() and wait() (and indirectly by communicate()). A None value indicates that the process hasn't terminated yet. A negative value -N indicates that the child was terminated by signal N (Unix only).

If the command exits with a non zero exitcode then you will get it.

And the cmd 'put' of hadoop should return 0(when success) and -1(when fail).

This explanation of why you get 255 rather -1 is quite simple and has already be explained here and here . Basically it is due to Java allowing signed 32 bits values as exit code (-1 for us) but a Posix exit status is an unsigned 8 bits value.

To summarize, a non-zero exit code will tell you that the command failed. If you want to check against a special exit code, special care must be taken when the code is not in the 0-255 range.

A return code of 255 means the Hadoop process exited with a -1 return code ( why that is I don't know).

The reason wait() doesn't give you -1 is because negative numbers are reserved for cases when the subprocess exited due to a signal (if it exited due to, say, signal 11, the return code would have been -11).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM