java获取Shell脚本执行信息及Shell脚本中调用java程序

1、java获取shell脚本执行信息

/home/oracle/auto_sh/jdk-linux/bin/java -classpath "/home/oracle/Temp_sh/Test_install.jar:/home/oracle/auto_sh/sqljdbc.jar"   com.test.Test 10.9.176.28 /home/oracle/Temp_sh/Test.sh

以上Shell脚本的意思:运行java程序"Test","com.test.Test"后的 IP及shell目录为"Test"类传的两参数。

以下java代码中获取shell脚本执行情况
public int executeSH(String sh)
{
int status = 1;
try {
Process cmd = Runtime.getRuntime().exec(sh);
InputStream in = cmd.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String tmp = null;
while((tmp=br.readLine())!=null)
{
if(tmp.length()>0)
{
System.out.println("== "+this.getCurrentTime()+" "+tmp);
}
if(tmp.equals("ByeBye"))
{
status = 0;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return status;

}

以下为shell脚本
/******************************************************
#! /bin/bash
echo "  ============================================================================="
echo "    *      *      * * * * *   *         * * *      * *     *       *   * * * * "
echo "     *     *     *  *         *        *         *     *   * *   * *   *       "
echo "      *   * *   *   * * * *   *        *         *     *   *  * *  *   * * * * "
echo "       * *   * *    *         *        *         *     *   *   *   *   *       "
echo "        *     *     * * * *   * * * *   * * *      * *     *   *   *   * * * * "
echo "  ============================================================================="
echo "  ============================== AUTO_INSTALL_START============================="
echo
Install_Path="/export/home/oracle/netnumen/TestInstall"
Install_Temp="/export/home/oracle/temp"
Version_IP="$1"
Version_Path="$2"
Pkg_name="$3"
Ftp_uname="$4"
Ftp_passwd="$5"
MainClass="com.test.ums"
function stop_testProc()
{
psid=0
for x in 1 2 3 4 5 7 8 9 10
do
   echo $x;
   javaps=`$Install_Path/ums/jdk-linux/bin/jps -l |grep $MainClass`
   #jps -l 获取java类"MainClass"进程
   #if ["$javaps" != '']; then
   if [ -n "$javaps" ]; then
     psid=`echo $javaps |awk '{print $1}'`
#取得java“MainClass”进程号
     echo $psid
     #su - oracle -c "kill -9 $psid"
     `kill -9 $psid`
     if [ $? -eq 0 ]; then
       echo "[Deleted OK]"
     else
       echo "[Deleted Failed]"
     fi
   else
     echo "[Now no need to kill process]"
     break
   fi
done
}
function silence_setup()
{
#unzip
unzip -o $Install_Temp/$Pkg_name  -d $Install_Temp/ >/export/home/oracle/auto_sh/unzip.log

#copy .properties
rm $Install_Temp/Installer/install/bin/test-silence-setup.properties
cp /export/home/oracle/auto_sh/test-silence-setup.properties $Install_Temp/Installer/install/bin/

#install
$Install_Temp/Installer/install/bin/solaris-test-silence-setup.sh >/export/home/oracle/auto_sh/test-silence-setup.log
        sleep 5
        if grep -qc "succeed" test-silence-setup.log;
#如果在日志中查找到“succeed”
               then echo "test-silence setup complete";
        else
               echo "test-silence setup fail";
               exit 1
        fi 
}
function stautup_testServer()
{
$Install_Path/ums/ums-svr/bin/console-solaris.sh &
for x in 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
do
sleep 20

p=` netstat -na |grep 64021`
if [ -n "$p" ]; then
        #如果字符串“$p”不为空
echo "############ Test startup test complete #############"
echo "ByeBye"
        sleep 10
echo "############   not  send udp msg complete  #############"
else
echo "!!!!!!!!!!!!!!!  starting  !!!!!!!!!!!!!!"
fi

done

suspend
}

echo "  -------------- [Del Install pkg] --------------"
chmod -R 755 $Install_Temp/Installer
rm $Install_Temp/$Pkg_name
rm -rf $Install_Temp/Installer
sleep 3
echo "  --------- [Del Install pkg complete] ----------"
echo
echo
echo "  -------------- [Get Install pkg] --------------"
ftp -v -n $Version_IP 21 <<EOF
user $Ftp_uname $Ftp_passwd
binary
cd $Test_Path
lcd $Install_Temp
prompt
get $Pkg_name $Test_Temp/$Pkg_name
close
bye
EOF
#ftp下载
echo "  -------- [Get Install pkg complete] -----------"
echo
echo
echo "  --------    [Stop running test]    --------"
#kill test porcess
stop_testProc
echo "  --------  [Stop running test complete] --------"
echo
echo
echo "  --------    [test-silence setup]    --------"
#install test porcess
silence_setup
echo "  --------  [test-silence setup complete] --------"
echo
echo
echo "  --------    [Startup testserver ...]    --------"
#start test server
stautup_testServer
echo
echo
sleep 1
echo "Bye "

/***************************************************************

你可能感兴趣的:(shell脚本)