1

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

Community
  • 1
  • 1
yimao
  • 11
  • 1
  • 1
  • 2

2 Answers2

4

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.

Community
  • 1
  • 1
Clément MATHIEU
  • 3,030
  • 23
  • 25
  • Thank you very much! Actually, May I know what situation the cmd 'put' of hadoop will return -1 ? I have checked that the files is uploads to hadoop from sever, with the same size. Thanks! – yimao Mar 22 '13 at 09:51
1

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).

robertklep
  • 198,204
  • 35
  • 394
  • 381