---
title: "有关linux启动脚本的浅析"
author: "pouyang"
date: "2011-11-14T22:36:53Z"
tags: []
categories: ["编程语言"]
slug: "linux"
draft: false
lastmod: "2011-11-15T11:10:47Z"
id: 1258917
url: "https://pouyang.iteye.com/blog/1258917"
---

有关linux启动脚本的浅析： 

 

1、参数脚本。

      #应用id，应用的唯一标识。

        APP\_NAME=APP\_01  

        APP\_DIR=../

        APP\_BIN=${APP\_DIR}/bin

        APP\_LIB=${APP\_DIR}/lib

        APP\_LOG=${APP\_DIR}/log

        APP\_SCRIPT=${APP\_DIR}/script

 

2、路径脚本。

        . para

       CLASSPATH=

       CLASSPATH=$CLASSPATH:${APP\_BIN}

       for jarfile in \`ls -1 ../lib/\*.jar\`

       do

           CLASSPATH="$CLASSPATH:$jarfile"

       done

       #export CLASSPATH

 

3、启动脚本。

        #! /bin/bash

        clear

        . para

        . class\_path

        #编码方式

        export LANG=zh\_CN.GB18030

        echo $LANG

        cd ${APP\_BIN}

        echo "${APP\_NAME} starting ..."

        sleep 1

        nohup java -Xms256m -Xmx512m -Xss256k -Dflag=${PLATFORM2\_NAME} -XX:PermSize=128m -XX:MaxPermSize=256m -Djava.security.policy="policy.txt" -Djava.rmi.server.codebase=file://${PLATFORM2\_BIN}/ -cp ${CLASSPATH} com.umpay.app.Start &gt;${APP\_LOG}/console.out &

        cd ${APP\_SCRIPT}

        #sleep 1

        tail -50f ${APP\_LOG}/console.out

 

4、停止脚本。

        4.1）、方式一

        #! /bin/bash

        sleep 1

        ps -ef|grep Dflag=${APP\_NAME}|grep -v grep|grep -v tail|awk 'BEGIN{printf "kill "}{printf "%s ", $2}'|bash

        echo "${APP\_NAME} stopped!

 

        4.2）、方式二

        #! /bin/bash

        . para

        c=0

        while \[ $c -eq 0 ]

        do

         echo "do" 

          result=\`ps -ef|grep "Dflag=${APP\_NAME}"|grep -v grep\`

          if \[ -n "${result}" ]

                then

                 echo $result

                 ps -ef|grep Dflag=${APP\_NAME}|grep -v grep|grep -v tail|awk 'BEGIN{printf "kill "}{printf "%s ", $2}'|bash

                 result=\`ps -ef|grep "Dflag=${APP\_NAME}"|grep -v grep\`

                 sleep 2

                else

                 c=1

                 echo "else"

         fi

        done 

        echo "结束"

 

 

4.3）、方式三

#! /bin/bash

. para

c=0

while \[ $c -eq 0 ]

do

 echo "do" 

  result=\`ps -ef|grep "Dflag=${PLATFORM2\_NAME}"|grep -v grep\`

  if \[ -n "${result}" ]

        then

         echo $result

         ps -ef|grep Dflag=${PLATFORM2\_NAME}|grep -v grep|grep -v tail|awk 'BEGIN{printf "kill "}{printf("%s", $2)}' | bash

         result=\`ps -ef|grep "Dflag=${PLATFORM2\_NAME}"|grep -v grep\`

         if \[ -n "${result}" ]

         then 

          ps -ef|grep Dflag=${PLATFORM2\_NAME}|grep -v grep|grep -v tail|awk 'BEGIN{printf "kill -9 "}{printf("%s", $2)}' | bash

         fi

         sleep 2

        else

         c=1

         echo "else"

 fi

done 

echo "结束

 

 

5、重启脚本。

        #! /bin/bash

        ./unserv.sh

        sleep 5

        ./start.sh

 

6、状态脚本。

        . para

        result=\`ps -ef|grep "Dflag=${APP\_NAME}"|grep -v grep\`

        if \[ -n "${result}" ]

        then

            echo "${APP\_NAME} is running."

        else

            echo "${APP\_NAME} not found..."

        fi

 

7、日志脚本。

        #! /bin/bash

        clear

        . para

        sleep 1

        tail -30f ${APP\_NAME}/console.out