linux脚本归纳

1.可选择输出到控制台还是后台运行

红色为注意更改路径

#!/bin/sh
#--------------------------------------------------------------
# sst.sh - Start/Stop script for SST
#
# Environment Variable Prequisties
#
#   JAVA_HOME     Must point at your Java Development Kit installation
#
#   Application_HOME      Point to your program
#
# --------------------------------------------------------------

Application_HOME="/home/pnsuser/PCRF_SMS_Mediator"
JAVA_HOME="/usr/java/jdk1.6.0_30"

Application_Name="PCRF_SMS_Mediator"
Main_Class="com.pccwmobile.pcrf.notification.main.PCRFNotificationMain"

cd $Application_HOME

if [ "$1" = "stop" ] ; then
        echo "Stopping $Application_Name Program ..."
        PIDLIST=`ps --width 8000 -fu $USER | grep -v "grep" | grep "$Main_Class" | cut -c 9-14`
        PID=`echo $PIDLIST | cut -c 1-5`
        if [ -n "$PID" ] ; then
                echo "Killing process $PID ..."
                kill -9 $PID
        else
                echo "$Application_Name Program is not running. Nothing to kill!"
        fi
        exit 1
elif [ "$1" = "status" ] ; then
        PIDLIST=`ps --width 8000 -fu $USER | grep -v "grep" | grep "$Main_Class" | cut -c 9-14`
        PID=`echo $PIDLIST | cut -c 1-5`
        if [ -n "$PID" ] ; then
                echo "$Application_Name Program is running (PID=$PID)"
        else
                echo "$Application_Name Program is not running."
        fi
        exit 1
elif [ "$1" = "run" ] ; then
        PIDLIST=`ps --width 8000 -fu $USER | grep -v "grep" | grep "$Main_Class" | cut -c 9-14`
        PID=`echo $PIDLIST | cut -c 1-5`
        if [ -n "$PID" ] ; then
                echo "$Application_Name Program is running (PID=$PID)"
                exit 1
        else
                for i in $Application_HOME/lib/*.jar
                do
                        if [ "$i" != "$Application_HOME/lib/*.jar" ] ; then
                                if [ -z "$LCP" ] ; then
                                        LCP=$i
                                else
                                        LCP="$i":"$LCP"
                                fi
                        fi
                done
                LCP=$Application_HOME/conf/:$LCP
                Application_CMD="$JAVA_HOME/bin/java -classpath $LCP:$Application_HOME/resource $Main_Class"
                echo $Application_CMD
                $Application_CMD
        fi
elif [ "$1" = "start" ] ; then
        PIDLIST=`ps --width 8000 -fu $USER | grep -v "grep" | grep "$Main_Class" | cut -c 9-14`
        PID=`echo $PIDLIST | cut -c 1-5`
        if [ -n "$PID" ] ; then
                echo "$Application_Name Program is running (PID=$PID)"
                exit 1
        else
                for i in $Application_HOME/lib/*.jar
                do
                        if [ "$i" != "$Application_HOME/lib/*.jar" ] ; then
                                if [ -z "$LCP" ] ; then
                                        LCP=$i
                                else
                                        LCP="$i":"$LCP"
                                fi
                        fi
                done
                LCP=$Application_HOME/conf/:$LCP
                Application_CMD="$JAVA_HOME/bin/java -classpath $LCP:$Application_HOME/resource $Main_Class"
                echo "$Application_CMD > /dev/null &"
                $Application_CMD > /dev/null &
        fi
else
        echo "Usage: ( run | start | stop | status)"
        echo "Commands:"
        echo "  run               Run $Application_Name program in current console"
        echo "  start             Start $Application_Name program as background process"
        echo "  stop              Stop $Application_Name program"
        echo "  status            Show the status of $Application_Name program"
        exit 1
fi
把 stdout 送到 /dev/null(无底洞) 里面,就不会输出到前台。

2.这是直接输出到控制台,是上面.sh的简化版
Application_HOME="/data/onepns/docroot/SMPP_HPNS/PCRF_SMS_Mediator"
JAVA_HOME="/usr/java/default/"


#-------------- Simple Grep -----------------------------
if [ "$1" = "stop" ] ; then
		echo "Stopping SMPPModule Program ..."
		PIDLIST=`ps --width 2000 -fu $USER | grep -v "grep" | grep "$Application_HOME" | cut -c9-14`
		PID=`echo $PIDLIST | cut -c0-5`
		if [ -n "$PID" ] ; then
			echo "Killing process $PID ..."
			kill -9 $PID
		else
			echo "PCRF_SMS_Mediator Program is not running. Nothing to kill!"
		fi
		exit 1

elif [ "$1" = "status" ] ; then
		PIDLIST=`ps --width 2000 -fu $USER | grep -v "grep" | grep "$Application_HOME" | cut -c9-14`
		PID=`echo $PIDLIST | cut -c0-5`
		if [ -n "$PID" ] ; then
			echo "PCRF_SMS_Mediator Program is running (PID=$PID)"
		else
			echo "PCRF_SMS_Mediator Program is not running."
		fi
		exit 1
fi


#-------------- The part below need java ---------------------
for i in $Application_HOME/lib/*.jar
do
  # if the directory is empty, then it will return the input string
  # this is stupid, so case for it
  if [ "$i" != "$Application_HOME/lib/*.jar" ] ; then
    if [ -z "$LCP" ] ; then
      LCP=$i
    else
      LCP="$i":"$LCP"
    fi
  fi
done

LCP=$Application_HOME/conf/:$LCP


echo "----------------------------------------"
echo Using JAVA_HOME: $JAVA_HOME
echo Using Application_HOME: $Application_HOME
echo "----------------------------------------"

#Application_CMD="$JAVA_HOME/bin/java -classpath $LCP com.pccwmobile.Test"
Application_CMD="$JAVA_HOME/bin/java -classpath $LCP:$Application_HOME/resource com.pccwmobile.pcrf.notification.main.PCRFNotificationMain"

echo $Application_CMD
$Application_CMD