Python测试进阶——(5)bash脚本启动Python监控程序并传递PID

发现HiBench执行Python监控程序脚本的命令为:

UID        PID  PPID  C STIME TTY          TIME CMD

root     32614     1  0 16:02 pts/0    00:00:00 python2 /home/cf/app/HiBench-master/bin/functions/monitor.py HadoopSort 32331 /home/cf/app/HiBench-master/report/sort/hadoop/conf/../monitor.log /home/cf/app/H

root     32621 32331  0 16:02 pts/0    00:00:00 python2 /home/cf/app/HiBench-master/bin/functions/execute_with_log.py /home/cf/app/HiBench-master/report/sort/hadoop/conf/../bench.log /opt/cloudera/parcels/CD

查看文件run.sh的内容:

 17 current_dir=`dirname "$0"`
 18 current_dir=`cd "$current_dir"; pwd`
 19 root_dir=${current_dir}/../../../../../
 20 workload_config=${root_dir}/conf/workloads/micro/sort.conf
 21 . "${root_dir}/bin/functions/load_bench_config.sh"
 22 
 23 enter_bench HadoopSort ${workload_config} ${current_dir}
 24 show_bannar start
 25 
 26 rmr_hdfs $OUTPUT_HDFS || true
 27 
 28 SIZE=`dir_size $INPUT_HDFS`
 29 START_TIME=`timestamp`
 30 run_hadoop_job ${HADOOP_EXAMPLES_JAR} sort -outKey org.apache.hadoop.io.Text -outValue org.apache.hadoop.io.Text -r ${NUM_REDS} ${INPUT_HDFS} ${OUTPUT_HDFS}
 31 
 32 END_TIME=`timestamp`
 33 gen_report ${START_TIME} ${END_TIME} ${SIZE}
 34 show_bannar finish
 35 leave_bench

在文件run.sh中,发现 run_hadoop_job() 调用了 start_monitor 方法:

function run_hadoop_job(){
    ENABLE_MONITOR=1
    if [ "$1" = "--without-monitor" ]; then
        ENABLE_MONITOR=0
        shift 1
    fi
    local job_jar=$1
    shift
    local job_name=$1
    shift
    local tail_arguments=$@
    local CMD="${HADOOP_EXECUTABLE} --config ${HADOOP_CONF_DIR} jar $job_jar $job_name $tail_arguments"
    echo -e "${BGreen}Submit MapReduce Job: ${Green}$CMD${Color_Off}"
    if [ ${ENABLE_MONITOR} = 1 ]; then
        MONITOR_PID=`start_monitor`
    fi
    execute_withlog ${CMD}
    result=$?
    if [ ${ENABLE_MONITOR} = 1 ]; then
        stop_monitor ${MONITOR_PID}
    fi
    if [ $result -ne 0 ]; then
        echo -e "${BRed}ERROR${Color_Off}: Hadoop job ${BYellow}${job_jar} ${job_name}${Color_Off} failed to run successfully."
        echo -e "${BBlue}Hint${Color_Off}: You can goto ${BYellow}${WORKLOAD_RESULT_FOLDER}/bench.log${Color_Off} to check for detailed log.\nOpening log tail for you:\n"
        tail ${WORKLOAD_RESULT_FOLDER}/bench.log
        exit $result
    fi
}

查看 start_monitor 方法的定义:

function start_monitor(){
    MONITOR_PID=`${workload_func_bin}/monitor.py ${HIBENCH_CUR_WORKLOAD_NAME} $$ ${WORKLOAD_RESULT_FOLDER}/monitor.log ${WORKLOAD_RESULT_FOLDER}/bench.log ${WORKLOAD_RESULT_FOLDER}/monitor.html ${SLAVES} &`
#    echo "start monitor, got child pid:${MONITOR_PID}" > /dev/stderr
    echo ${MONITOR_PID}
}

还有 stop_monitor  方法的定义:

function stop_monitor(){
    MONITOR_PID=$1
    assert $1 "monitor pid missing"
#    echo "stop monitor, kill ${MONITOR_PID}" > /dev/stderr
    kill ${MONITOR_PID}
}

 以及 execute_withlog 方法的定义:

function execute_withlog () {
    CMD="$@"
    if [ -t 1 ] ; then          # Terminal, beautify the output.
        ${workload_func_bin}/execute_with_log.py ${WORKLOAD_RESULT_FOLDER}/bench.log $CMD
    else                        # pipe, do nothing.
        $CMD
    fi
}

猜你喜欢

转载自www.cnblogs.com/ratels/p/11070262.html
今日推荐