shell脚本没有完全从java

时间:2015-11-29 14:06:35

标签: java sql shell unix spool

我有一个shell脚本,可以执行sql build job。

当从unix提示手动调用它时,它正在执行并按预期给出结果。

但是当从java调用它时,它并没有完全执行。

手动执行:

MODIFY_PATCH_CREATION.ksh 118765 CP_14052906_28112015134449_16

日志文件显示为:

Installing Patch for BATCH ID:118765 and PATCH NAME:CP_14052906_28112015134449_16
CP_14052906_28112015134449_16_RELEASE directory has been created
CP_14052906_28112015134449_16_INSTALL directory has been created
CP_14052906_28112015134449_16_ROLLBACK directory has been created
SQL and LOG directories have been created for INSTALL
INSTALL script is created
SQL and LOG directories have been created for ROLLBACK
ROLLBACK script is created
SQL files are created for INSTALL
SQL files are created for ROLLBACK
Patch installed Successfully..

但是如果我从下面的java程序中执行相同的操作:

java代码:

public class ShellTest{


        public static void main(String a[]) throws Exception{


                System.out.println("Hello kp");
                Runtime.getRuntime().exec("MODIFY_PATCH_CREATION.ksh 118765 CP_14052906_28112015134449_16");


        }

}

当作为Java ShellTest执行时,日志只达到某种程度

Installing Patch for BATCH ID:118765 and PATCH NAME:CP_14052906_28112015134449_16
CP_14052906_28112015134449_16_RELEASE directory has been created
CP_14052906_28112015134449_16_INSTALL directory has been created
CP_14052906_28112015134449_16_ROLLBACK directory has been created
SQL and LOG directories have been created for INSTALL
INSTALL script is created
SQL and LOG directories have been created for ROLLBACK
ROLLBACK script is created
SQL files are created for INSTALL

Shell脚本代码:

#!/bin/bash
BATCH_ID=$1
echo "BATCH_ID: $BATCH_ID"
PATCH_NAME=$2
echo "PATCH_NAME: $PATCH_NAME"

echo "Creating patch for batch id $BATCH_ID"

ARBOR_USR=`echo $K_CAT_USERNAME`
dateTime=`date '+%d%m%Y_%H%M%S'`

PATCH_DIR=$ARBORDIR/cw/CPF_GSA_Modify
RELEASE_DIR=$ARBORDIR/cw/CPF_GSA_Modify/$2_RELEASE
INSTALL_DIR=$ARBORDIR/cw/CPF_GSA_Modify/$2_RELEASE/$2_INSTALL/
ROLLBACK_DIR=$ARBORDIR/cw/CPF_GSA_Modify/$2_RELEASE/$2_ROLLBACK/

##Logfile creation for install
cd $PATCH_DIR
touch $2_PATCH_log_$dateTime.txt
log_file=$PATCH_DIR/$2_PATCH_log_$dateTime.txt
echo "Installing Patch for BATCH ID:$BATCH_ID and PATCH NAME:$PATCH_NAME" >>$log_file

mkdir $2_RELEASE
echo "$2_RELEASE directory has been created" >>$log_file

cd $2_RELEASE
mkdir $2_INSTALL
echo "$2_INSTALL directory has been created" >>$log_file
mkdir $2_ROLLBACK
echo "$2_ROLLBACK directory has been created" >>$log_file

cd $INSTALL_DIR
mkdir SQL
mkdir LOG
echo "SQL and LOG directories have been created for INSTALL" >>$log_file
cp $PATCH_DIR/vge_cpf_gsa_mod_db_install.ksh $2_install.ksh
sed -i "s/vge_cpf_gsa_mod_db/$2/g" $2_install.ksh
echo "INSTALL script is created" >>$log_file
#cd SQL
#touch sql_list.sql

cd $ROLLBACK_DIR
mkdir SQL
mkdir LOG
echo "SQL and LOG directories have been created for ROLLBACK" >>$log_file
cp $PATCH_DIR/vge_cpf_gsa_mod_db_rollback.ksh $2_rollback.ksh
sed -i "s/vge_cpf_gsa_mod_db/$2/g" $2_rollback.ksh
echo "ROLLBACK script is created" >>$log_file
#cd SQL
touch sql_list.sql

    cd $INSTALL_DIR/SQL/
    echo 'Going to Connect to database'
    touch display.log
    sqlplus $ARBOR_USR/`cat $ARBORDIR/.arborpw`@$ORACLE_SID << THEEND >>display.log
    set define off;
    set linesize 3000;
    set head off;
    set feedback off;

    spool CW_NEW_MRC.sql;
    SELECT 'UPDATE RATE_RC a SET INACTIVE_DATE= (SELECT DISTINCT active_date  FROM  CW_PC_RR b WHERE a.element_id=b.element_id AND a.component_id=b.component_id and rownum<2) WHERE ELEMENT_ID='||MEMBER_ID||' AND COMPONENT_ID= '||COMPONENT_ID||' AND  INACTIVE_date IS NULL ;' STATEMENT FROM CW_OLD_MRC_RATES WHERE batch_id=$BATCH_ID;
    select 'INSERT INTO "RATE_RC" ("RATE_CLASS", "ELEMENT_ID", "COMPONENT_ID", "EQUIP_TYPE_CODE", "EQUIP_CLASS_CODE", "CLASS_OF_SERVICE_CODE", "TYPE_ID_RC", "BILLING_FREQUENCY", "CURRENCY_CODE", "RATE", "JURISDICTION", "DISTANCE_INCREMENT", "UNITS_TYPE", "UNITS_LOWER_LIMIT", "UNITS_UPPER_LIMIT", "UNITS_RATE", "POP_LOWER_LIMIT", "POP_UPPER_LIMIT", "DISTANCE_RATE", "ADD_IMPLIED_DECIMALS", "DATE_CREATED", "ACTIVE_DATE", "INACTIVE_DATE", "TEST_FLAG", "FOREIGN_CODE")VALUES ( '''|| RATE_CLASS||''','''||ELEMENT_ID||''','''||COMPONENT_ID||''','''||EQUIP_TYPE_CODE||''','''||EQUIP_CLASS_CODE||''','''||CLASS_OF_SERVICE_CODE||''','''||TYPE_ID_RC||''','''||BILLING_FREQUENCY||''','''||CURRENCY_CODE||''','''||RATE||''','''||JURISDICTION||''','''||DISTANCE_INCREMENT||''','''||UNITS_TYPE||''','''||UNITS_LOWER_LIMIT||''','''||UNITS_UPPER_LIMIT||''','''||UNITS_RATE||''','''||POP_LOWER_LIMIT||''','''||POP_UPPER_LIMIT||''','''||DISTANCE_RATE||''','''||ADD_IMPLIED_DECIMALS||''','''||to_char(DATE_CREATED ,'dd-mon-yyyy')||''','''||to_char(ACTIVE_DATE , 'dd-mon-yyyy')||''','''||to_char(INACTIVE_DATE , 'dd-mon-yyyy')||''','''||TEST_FLAG||''','''||FOREIGN_CODE || '''); ' statement from CW_PC_RR where batch_id=$BATCH_ID;
    spool CW_NEW_DISCOUNT.sql;
       SELECT 'UPDATE RATE_DISCOUNT RDIS SET RDIS.DATE_INACTIVE= (SELECT DISTINCT date_active  FROM  CW_PC_RD b WHERE RDIS .discount_id=b.discount_id and rownum<2) WHERE DISCOUNT_ID='||CR.DISCOUNT_ID|| ' AND billing_frequency= '||billing_frequency||' AND currency_code= '||currency_code||' AND  DATE_INACTIVE IS NULL;'  STATEMENT FROM CW_CPF_MODIFY_DISCOUNT_RATES CR where batch_id=$BATCH_ID;    
    SELECT 'INSERT INTO "RATE_DISCOUNT" ("DISCOUNT_ID", "RATE_CLASS", "RANGE_ORIGIN", "RANGE_TERMINUS", "BILLING_FREQUENCY", "CURRENCY_CODE", "DISCOUNT_PERCENT", "IMPLIED_DECIMALS", "DISCOUNT_AMOUNT", "DATE_ACTIVE", "DATE_INACTIVE", "DATE_CREATED", "LOOKUP_THRESHOLD_TIER_ID", "QUALIFYING_THRESHOLD_TIER_ID") VALUES ('''||DISCOUNT_ID||''','''|| RATE_CLASS||''','''||RANGE_ORIGIN||''','''||RANGE_TERMINUS||''','''||BILLING_FREQUENCY||''','''||CURRENCY_CODE||''','''||DISCOUNT_PERCENT||''','''||IMPLIED_DECIMALS||''','''||DISCOUNT_AMOUNT||''','''||to_char(DATE_ACTIVE , 'dd-mon-yyyy')||''','''||'' ||''','''||to_char(DATE_ACTIVE , 'dd-mon-yyyy')||''','''||LOOKUP_THRESHOLD_TIER_ID||''','''||QUALIFYING_THRESHOLD_TIER_ID||''');' statement from CW_PC_RD where batch_id=$BATCH_ID;
    spool CW_NEW_UNIT_CREDIT.sql;
       SELECT 'UPDATE RATE_UNIT_CR RDIS SET RDIS.DATE_INACTIVE = (SELECT DISTINCT date_active  FROM  CW_PC_RUC b WHERE b.unit_cr_id=rdis.UNIT_CR_ID and rownum<2)   WHERE UNIT_CR_ID='||CR.DISCOUNT_ID|| ' AND billing_frequency= '||billing_frequency||' AND DATE_INACTIVE IS NULL;'  STATEMENT FROM CW_CPF_MODIFY_UNITCREDIT_RATES CR where batch_id=$BATCH_ID;
    SELECT 'INSERT INTO "RATE_UNIT_CR" ("UNIT_CR_ID", "RATE_CLASS", "RANGE_ORIGIN", "RANGE_TERMINUS", "BILLING_FREQUENCY", "CURRENCY_CODE", "UNITS_FREE", "AMOUNT_FREE", "DATE_ACTIVE", "DATE_INACTIVE") VALUES ('''||UNIT_CR_ID||''','''||RATE_CLASS||''','''||RANGE_ORIGIN||''','''||RANGE_TERMINUS||''','''||BILLING_FREQUENCY||''','''||CURRENCY_CODE||''','''||UNITS_FREE||''','''||AMOUNT_FREE||''','''||to_char(DATE_ACTIVE , 'dd-mon-yyyy')||''','''||to_char(DATE_ACTIVE , 'dd-mon-yyyy')||''');' statement from CW_PC_RUC where batch_id=$BATCH_ID;
    spool CW_NEW_TARIFF_RATE.sql;
    SELECT 'UPDATE TARIFF_RATE TR SET TR.DATE_INACTIVE = (SELECT DISTINCT date_active FROM CW_PC_TR b where tr.tariff_id=b.tariff_id and tr.bundle_id=b.bundle_id and rownum<2) WHERE TARIFF_ID='||CR.TARIFF_ID||' AND BUNDLE_ID='||CR.BUNDLE_ID||' AND DATE_INACTIVE IS NULL;' STATEMENT FROM CW_OLD_TARIFF_SELF_OPT_RATES CR;
    SELECT 'INSERT INTO "TARIFF_RATE" ("TARIFF_ID", "BUNDLE_ID","CHARGE_AMOUNT","CURRENCY_CODE","IMPLIED_DECIMALS","DATE_ACTIVE","DATE_CREATED") VALUES ('''||TARIFF_ID||''','''||BUNDLE_ID||''','''||CHARGE_AMOUNT||''','''||CURRENCY_CODE||''','''||IMPLIED_DECIMALS||''','''||DATE_ACTIVE||''','''||DATE_CREATED||''');' statement from CW_PC_TR where batch_id=$BATCH_ID;
    SELECT 'UPDATE TARIFF_RATE_BAND TRB SET TRB.STEP='||DECODE(STEP,'','NULL',STEP)||',TRB.RANGE_ORIGIN='||DECODE(RANGE_ORIGIN,'','NULL',RANGE_ORIGIN)||',TRB.RANGE_TERMINUS='||DECODE(RANGE_TERMINUS,'','NULL',RANGE_TERMINUS)||' WHERE TRB.TARIFF_ID='||TARIFF_ID||' AND TRB.AGGREGATION_KEY='||AGGREGATION_KEY||' AND TRB.BUNDLE_ID='||BUNDLE_ID||';' STATEMENT FROM CW_NEW_TARIFF_SELF_OPT_RATES where batch_id=$BATCH_ID;
        spool CW_NEW_USAGE_RATES.sql;
    SELECT  'UPDATE RATE_USAGE RU SET RU.INACTIVE_DT= (SELECT DISTINCT active_dt FROM CW_PC_RU b where ru.component_id=b.component_id and ru.type_id_usg=b.type_id_usg and ru.jurisdiction=b.jurisdiction and rownum<2 ) WHERE SEQNUM='||SEQNUM||'AND INACTIVE_DT IS NULL;' STATEMENT FROM CW_OLD_USAGE_RATES WHERE batch_id=$BATCH_ID; 
    select 'INSERT INTO "RATE_USAGE" ("SEQNUM", "RATE_CLASS", "TYPE_ID_USG", "BILL_CLASS","JURISDICTION","POINT_CLASS_TARGET","POINT_CLASS_ORIGIN","UNITS_INDICATOR","PROVIDER_CLASS","RATE_PERIOD","CURRENCY_CODE","ELEMENT_ID","EQUIP_TYPE_CODE","EQUIP_CLASS_CODE","CLASS_OF_SERVICE_CODE","FIXED_CHARGE_AMT","ADD_FIXED_AMT","ADD_UNIT_RATE","ACTIVE_DT","INACTIVE_DT","CREATE_DT","CHANGE_DT","CHANGE_WHO","INCREMENTAL_BANDS","RATE_UNITS_TYPE","DISTANCE_BAND_ID","ZONE_CLASS","ADD_IMPLIED_DECIMAL","IS_DEFAULT_RATE","COMPONENT_ID","BILLING_UNITS_TYPE","MIN_CHARGE_AMT","ADD_MIN_AMT") VALUES ('''||SEQNUM||''','''||RATE_CLASS||''','''||TYPE_ID_USG||''','''||BILL_CLASS||''','''||JURISDICTION||''','''||POINT_CLASS_TARGET||''','''||POINT_CLASS_ORIGIN||''','''||UNITS_INDICATOR||''','''||PROVIDER_CLASS||''','''||RATE_PERIOD||''','''||CURRENCY_CODE||''','''||ELEMENT_ID||''','''||EQUIP_TYPE_CODE||''','''||EQUIP_CLASS_CODE||''','''||CLASS_OF_SERVICE_CODE||''','''||FIXED_CHARGE_AMT||''','''||ADD_FIXED_AMT||''','''||ADD_UNIT_RATE||''','''||ACTIVE_DT||''','''||INACTIVE_DT||''','''||CREATE_DT||''','''||CHANGE_DT||''','''||CHANGE_WHO||''','''||INCREMENTAL_BANDS||''','''||RATE_UNITS_TYPE||''','''||DISTANCE_BAND_ID||''','''||ZONE_CLASS||''','''||ADD_IMPLIED_DECIMAL||''','''||IS_DEFAULT_RATE||''','''||COMPONENT_ID||''','''||BILLING_UNITS_TYPE||''','''||MIN_CHARGE_AMT||''','''||ADD_MIN_AMT || ''') ;' statement from CW_PC_RU where batch_id=$BATCH_ID;
    select 'INSERT INTO "RATE_USAGE_BANDS" ("SEQNUM", "RATEBAND", "UNIT_RATE", "NUM_UNITS") VALUES ('''||SEQNUM||''','''||RATEBAND||''','''||UNIT_RATE||''','''||NUM_UNITS||''') ;' statement from CW_PC_RUB where batch_id=$BATCH_ID ORDER BY RATEBAND ASC;
    spool CW_NEW_FUP.sql
    SELECT 'UPDATE CW_VGE_FUP_REF_DATA SET TOLERANCE='''||TOLERANCE||''',PRICE_DELTA='''||PRICE_DELTA||''',FUP_LOWER='''||FUP_LOWER||''',ALLOWANCE='''||ALLOWANCE||'''  WHERE UNIT_CR_ID='||UNIT_CR_ID|| ';'  STATEMENT FROM CW_PC_FUP where batch_id=$BATCH_ID;
    spool off


    quit
THEEND
echo "SQL files are created for INSTALL" >>$log_file

echo "@CW_NEW_MRC.sql" >>sql_list.sql
echo "@CW_NEW_DISCOUNT.sql" >>sql_list.sql
echo "@CW_NEW_UNIT_CREDIT.sql" >>sql_list.sql
echo "@CW_NEW_TARIFF_RATE.sql" >>sql_list.sql
echo "@CW_NEW_USAGE_RATES.sql" >>sql_list.sql
echo "@CW_NEW_FUP.sql" >>sql_list.sql

    cd $ROLLBACK_DIR/SQL/

    echo 'Going to Connect to database'
    touch display.log
    sqlplus $ARBOR_USR/`cat $ARBORDIR/.arborpw`@$ORACLE_SID << THEEND >>display.log
    clear buffer;
    set define off;
    set linesize 3000;
    set head off;
    set feed off
    set feedback off;
    COLUMN HEADING OFF
    column HEADER truncated
    column FOOTER truncated

    spool CW_OLD_MRC.sql;
    SELECT 'DELETE FROM RATE_RC  WHERE INACTIVE_DATE=NULL AND  ELEMENT_ID=' ||ELEMENT_ID||' AND COMPONENT_ID='||COMPONENT_ID|| '; ' STATEMENT FROM CW_PC_RR where batch_id=$BATCH_ID;
    SELECT 'UPDATE RATE_RC SET INACTIVE_DATE=NULL  WHERE ELEMENT_ID='||MEMBER_ID||' AND COMPONENT_ID= '||COMPONENT_ID||';' STATEMENT FROM CW_OLD_MRC_RATES where batch_id=$BATCH_ID;

    spool CW_OLD_DISCOUNT.sql;
    SELECT 'DELETE FROM RATE_DISCOUNT  WHERE INACTIVE_DATE=NULL AND  DISCOUNT_ID=' ||DISCOUNT_ID|| '; ' STATEMENT FROM CW_PC_RD where batch_id=$BATCH_ID;
    SELECT 'UPDATE RATE_DISCOUNT RDIS SET RDIS.DATE_INACTIVE= null WHERE DISCOUNT_ID='||CR.DISCOUNT_ID|| ';'  STATEMENT FROM CW_CPF_MODIFY_DISCOUNT_RATES CR where batch_id=$BATCH_ID;  

    spool CW_OLD_UNIT_CREDIT.sql;
    SELECT 'DELETE FROM RATE_UNIT_CR  WHERE INACTIVE_DATE=NULL AND  UNIT_CR_ID=' ||UNIT_CR_ID|| '; ' STATEMENT FROM CW_PC_RUC where batch_id=$BATCH_ID;
    SELECT 'UPDATE RATE_UNIT_CR RDIS SET RDIS.DATE_INACTIVE= null WHERE UNIT_CR_ID='||CR.DISCOUNT_ID|| ';'  STATEMENT FROM CW_CPF_MODIFY_UNITCREDIT_RATES CR where batch_id=$BATCH_ID;

    spool CW_OLD_TARIFF_RATE.sql;
    SELECT 'DELETE FROM TARIFF_RATE WHERE DATE_INACTIVE= NULL  AND  TARIFF_ID='||CR.TARIFF_ID||' AND BUNDLE_ID='||CR.BUNDLE_ID||'; ' STATEMENT FROM CW_PC_TR CR where batch_id=$BATCH_ID;
    SELECT 'UPDATE TARIFF_RATE TR SET TR.DATE_INACTIVE= NULL WHERE TARIFF_ID='||CR.TARIFF_ID||' AND BUNDLE_ID='||CR.BUNDLE_ID||' ;' STATEMENT FROM CW_OLD_TARIFF_SELF_OPT_RATES CR where batch_id=$BATCH_ID;
    select 'UPDATE TARIFF_RATE_BAND TRB SET TRB.STEP='||decode(STEP,'','NULL',STEP)||',TRB.RANGE_ORIGIN='||decode(RANGE_ORIGIN,'','NULL',RANGE_ORIGIN)||',TRB.RANGE_TERMINUS='||decode(RANGE_TERMINUS,'','NULL',RANGE_TERMINUS)||' WHERE TRB.TARIFF_ID='||TARIFF_ID||' AND TRB.AGGREGATION_KEY='||AGGREGATION_KEY||' AND TRB.BUNDLE_ID='||BUNDLE_ID||';' statement from CW_OLD_TARIFF_SELF_OPT_RATES where batch_id=$BATCH_ID;

    spool CW_OLD_USAGE_RATES.sql;
    SELECT 'DELETE FROM RATE_USAGE WHERE SEQNUM = '||RU.SEQNUM|| ';' STATEMENT FROM CW_PC_RU RU where batch_id=$BATCH_ID;
    SELECT 'DELETE FROM RATE_USAGE_BANDS WHERE SEQNUM = '||RUB.SEQNUM|| ';' STATEMENT FROM CW_PC_RUB RUB where batch_id=$BATCH_ID;
    SELECT  'UPDATE RATE_USAGE RU SET RU.INACTIVE_DT= NULL WHERE SEQNUM='||SEQNUM||';' STATEMENT FROM CW_OLD_USAGE_RATES where batch_id=$BATCH_ID;


    spool off

    UPDATE CW_PC_BATCH_STATUS SET STATUS = 'PATCH_CREATED' WHERE BATCH_ID=$BATCH_ID;

    quit


THEEND
echo "SQL files are created for ROLLBACK" >>$log_file

echo "@CW_OLD_MRC.sql" >>sql_list.sql
echo "@CW_OLD_DISCOUNT.sql" >>sql_list.sql
echo "@CW_OLD_UNIT_CREDIT.sql" >>sql_list.sql
echo "@CW_OLD_TARIFF_RATE.sql" >>sql_list.sql
echo "@CW_OLD_USAGE_RATES.sql" >>sql_list.sql


echo "Patch installed Successfully..">>$log_file
tar -cf $PATCH_DIR/$PATCH_NAME.tar $RELEASE_DIR
cp $PATCH_DIR/$PATCH_NAME.tar $ARBORDIR/cw/delivery
对我来说很奇怪。请帮忙。

1 个答案:

答案 0 :(得分:1)

该脚本具有echo语句,这些语句不是针对日志的,但是您的Java程序没有消耗脚本进程的输出,因此一旦输出缓冲区已满,脚本就会停止。

如果您希望脚本输出转到控制台,即与Java程序输出相同的输出目标,则需要使用ProcessBuilder.inheritIO()

无论如何你应该使用ProcessBuilder。引用Process的javadoc:

  

默认情况下,创建的子进程没有自己的终端或控制台。其所有标准I / O(即stdin,stdout,stderr)操作将被重定向到父进程,在那里可以通过使用方法getOutputStream()getInputStream()和{{获得的流来访问它们。 3}}。父进程使用这些流向子进程提供输入并从子进程获取输出。由于某些本机平台仅为标准输入和输出流提供有限的缓冲区大小,无法及时写入输入流或读取子流程的输出流可能导致子流程阻塞,甚至死锁。

     

如果需要,getErrorStream()使用subprocess I/O can also be redirected类的方法。

     

从1.5开始,ProcessBuilder是创建Process的首选方式。