0

我试图通过 python 子进程运行 JDK 自解压 .bin 文件,

这是代码

proc = subprocess.Popen('./jdk-6u45-linux-i586.bin', cwd=installdir,shell=True, stdout=subprocess.PIPE)

它运行并卡住,输出显示如下:

Install jdk-6u45-linux-i586.bin ...
Unpacking...

bin文件能够在终端中独立运行

我通过以下方式打开 shell 调试

proc = subprocess.Popen('sh -x ./jdk-6u45-linux-i586.bin', cwd=installdir,shell=True, stdout=subprocess.PIPE)

输出如下

Install jdk-6u45-linux-i586.bin ...
Unpacking...

+ PATH=/usr/bin:/bin

Checksumming...

+ umask 022

Extracting...

+ javahome=jdk1.6.0_45

UnZipSFX 5.50 of 17 February 2002, by Info-ZIP (Zip-Bugs@lists.wku.edu).

+ platform=linux

   creating: jdk1.6.0_45/

+ PACKED_JARS='jre/lib/rt.jar jre/lib/jsse.jar jre/lib/charsets.jar lib/tools.jar jre/lib/ext/localedata.jar jre/lib/plugin.jar jre/lib/javaws.jar jre/lib/deploy.jar'

   creating: jdk1.6.0_45/jre/

+ ARCH=32

   creating: jdk1.6.0_45/jre/bin/

+ BINARY_NAME=jdk-6u45-fcs-bin-b06-linux-i586-26_mar_2013.bin

  inflating: jdk1.6.0_45/jre/bin/java

+ diskSpaceRequired=230500

  inflating: jdk1.6.0_45/jre/bin/keytool

++ uname -s

  inflating: jdk1.6.0_45/jre/bin/policytool

+ '[' Linux = Linux ']'

  inflating: jdk1.6.0_45/jre/bin/rmiregistry

+ tail_args=-n

  inflating: jdk1.6.0_45/jre/bin/rmid

+ outname=install.sfx.5699

  inflating: jdk1.6.0_45/jre/bin/tnameserv

++ df -k .

  inflating: jdk1.6.0_45/jre/bin/orbd

++ awk '{if ( $4 ~ /%/) { print $3 } else { print $4 } }'

  inflating: jdk1.6.0_45/jre/bin/servertool

++ tail -n -1

  inflating: jdk1.6.0_45/jre/bin/unpack200

+ diskSpace=540468

  inflating: jdk1.6.0_45/jre/bin/pack200

+ '[' 540468 -lt 230500 ']'

  inflating: jdk1.6.0_45/jre/bin/jcontrol

+ trap 'rm -f $outname; exit 1' HUP INT QUIT TERM

    linking: jdk1.6.0_45/jre/bin/ControlPanel  -> ./jcontrol

+ echo Unpacking...

  inflating: jdk1.6.0_45/jre/bin/java_vm

+ tail -n +146 ./jdk-6u45-linux-i586.bin

  inflating: jdk1.6.0_45/jre/bin/javaws

+ '[' -x /usr/bin/sum ']'

   creating: jdk1.6.0_45/jre/lib/

+ echo Checksumming...

   creating: jdk1.6.0_45/jre/lib/applet/

++ /usr/bin/sum install.sfx.5699

   creating: jdk1.6.0_45/jre/lib/ext/

+ sum='01464 70113'

  inflating: jdk1.6.0_45/jre/lib/ext/sunjce_provider.jar

+ index=1

  inflating: jdk1.6.0_45/jre/lib/ext/sunpkcs11.jar

+ for s in '$sum'

  inflating: jdk1.6.0_45/jre/lib/ext/dnsns.jar

+ case $index in

  inflating: jdk1.6.0_45/jre/lib/ext/localedata.pack

+ sum1=01464

  inflating: jdk1.6.0_45/jre/lib/ext/meta-index

+ index=2

   creating: jdk1.6.0_45/jre/lib/i386/

+ for s in '$sum'

   creating: jdk1.6.0_45/jre/lib/i386/native_threads/

+ case $index in

  inflating: jdk1.6.0_45/jre/lib/i386/native_threads/libhpi.so

+ sum2=70113

   creating: jdk1.6.0_45/jre/lib/i386/server/

+ index=3

  inflating: jdk1.6.0_45/jre/lib/i386/server/libjvm.so

+ '[' 01464 -ne 01464 ']'

  inflating: jdk1.6.0_45/jre/lib/i386/server/Xusage.txt

+ '[' 70113 -ne 70113 ']'

    linking: jdk1.6.0_45/jre/lib/i386/server/libjsig.so  -> ../libjsig.so

+ chmod +x install.sfx.5699

  inflating: jdk1.6.0_45/jre/lib/i386/libsaproc.so

+ echo Extracting...

   creating: jdk1.6.0_45/jre/lib/i386/client/

+ ./install.sfx.5699

  inflating: jdk1.6.0_45/jre/lib/i386/client/libjvm.so 

我还通过以下方式捕获了 bin 文件的自提取脚本

head -145 jdk-6u45-linux-i586.bin

从脚本和调试日志中,我可以看到它只是停留在提取脚本的第 74 行

echo "Extracting..."
./$outname

完整脚本如下:

#!/bin/sh
PATH=/usr/bin:/bin
umask 022

release_comp() {
    if [ "$1" = "$2" ] ; then
    echo "eq"
    else
    lrel=`printf "%s\n%s\n" $1 $2 | \
        sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n -k 5,5n | \
        head -1`
        if [ "$1" = "${lrel}" ]; then
            echo "lt"
        else
            echo "gt"
        fi
    fi
}


install_JavaDB() {
    return 0
}

javahome=jdk1.6.0_45
platform=linux
PACKED_JARS="jre/lib/rt.jar jre/lib/jsse.jar jre/lib/charsets.jar lib/tools.jar jre/lib/ext/localedata.jar jre/lib/plugin.jar jre/lib/javaws.jar jre/lib/deploy.jar"
ARCH=32
BINARY_NAME=jdk-6u45-fcs-bin-b06-linux-i586-26_mar_2013.bin
diskSpaceRequired=230500
if [ "`uname -s`" = "Linux" ]; then
    tail_args="-n"
fi

outname=install.sfx.$$
diskSpace=`df -k . | tail ${tail_args} -1 | awk '{if ( $4 ~ /%/) { print $3 } else { print $4 } }'`
if [ $diskSpace -lt $diskSpaceRequired ]; then
    printf "You will need atleast %s kBytes of Disk Free\n" $diskSpaceRequired
    printf "Please free up the required Disk Space and try again\n"
    exit 3
fi
trap 'rm -f $outname; exit 1' HUP INT QUIT TERM
echo "Unpacking..."
tail ${tail_args} +146 "$0" > $outname
if [ -x /usr/bin/sum ]; then
    echo "Checksumming..."

    sum=`/usr/bin/sum $outname`
    index=1
    for s in $sum; do
        case $index in
            1)
                sum1=$s;
                index=2;
                ;;
            2)
                sum2=$s;
                index=3;
                ;;
        esac
    done
    if [ $sum1 -ne 01464 ] || [ $sum2 -ne 70113 ]; then
        echo "The download file appears to be corrupted.  Please refer"
        echo "to the Troubleshooting section of the Installation"
        echo "Instructions on the download page for more information."
        echo "Please do not attempt to install this archive file."
        exit 1
    fi
else
    echo "Can't find /usr/bin/sum to do checksum.  Continuing anyway."
fi
chmod +x $outname
echo "Extracting..."
./$outname
rm -f $outname #we dont need it anymore, so conserve disk space


if [ -z "${RPM_NAME}" ]; then
    if [ ! -d ${javahome} ]; then
        echo "Failed to extract the files.  Please refer to the Troubleshooting section of"
        echo "the Installation Instructions on the download page for more information."
        exit 1
    fi

    UNPACK_EXE=$javahome/bin/unpack200
    if [ -f $UNPACK_EXE ]; then
        chmod +x $UNPACK_EXE 
        packerror=""
        for i in $PACKED_JARS; do
            if [ -f $javahome/`dirname $i`/`basename $i .jar`.pack ]; then
                printf "Creating %s\n" $javahome/$i
                $UNPACK_EXE $javahome/`dirname $i`/`basename $i .jar`.pack $javahome/$i
                if [ $? -ne 0 ] || [ ! -f $javahome/$i ]; then
                    printf "ERROR: Failed to unpack JAR file:\n\n\t%s\n\n" $i
            printf "Installation failed. Please refer to the Troubleshooting Section of\n"
            printf "the Installation Instructions on the download page.\n"
                    packerror="1"
            break
                fi

        # remove the old pack file
        rm -f $javahome/`dirname $i`/`basename $i .jar`.pack
            fi
        done
        if [  "$packerror" = "1" ]; then 
            if [ -d $javahome ]; then
                /bin/rm -rf $javahome
            fi

        exit 2
        fi
    fi

    PREFS_LOCATION="`echo \"${javahome}\" | sed -e 's/^jdk.*/&\/jre/'`/.systemPrefs"
    if [ ! -d "${PREFS_LOCATION}" ]; then
        mkdir -m 755 "${PREFS_LOCATION}"
    fi
    if [ ! -f "${PREFS_LOCATION}/.system.lock" ]; then
        touch "${PREFS_LOCATION}/.system.lock"
        chmod 644 "${PREFS_LOCATION}/.system.lock"
    fi
    if [ ! -f "${PREFS_LOCATION}/.systemRootModFile" ]; then
        touch "${PREFS_LOCATION}/.systemRootModFile"
        chmod 644 "${PREFS_LOCATION}/.systemRootModFile"
    fi

    if [ x$ARCH = "x32" ] && [ -f "$javahome/bin/java" ]; then
        "$javahome/bin/java" -client -Xshare:dump > /dev/null 2>&1

    fi

else
    if [ "$1" = "-x" ]; then
        echo "Extraction of RPM Done."
    else
        #  If rpm and the file exists then use rpm to install.
        if [ -f `pwd`/$RPM_NAME ]; then
            rpm -ivh `pwd`/$RPM_NAME
        fi
    install_JavaDB
    fi
fi
echo " "
echo "Done."
exit 0

关于上述问题的任何想法,我猜有些环境没有传入 python,无法弄清楚它是什么。因为 bin 可以直接运行,但不能通过 python 子进程运行

4

1 回答 1

0

这个线程实际上回答了这个问题:

在 python 中对 subprocess.PIPE 进行非阻塞读取

它确实解决了我的问题。

谢谢

于 2014-08-24T06:04:08.680 回答