wKioL1jKLuySD0poAABm6i-XhL0570.jpg

  用来获取系统中运行的java进程,这里针对 tomcat。打印出部署的路径信息,重启后的监听状态。

  查看fint帮助

[view@payqa1 ~]$ fint -h
Usage: findTom [OPTION]
View the tomcat information.
View the home directory of the tomcat program running on the system, and
the project path.

Mandatory arguments to long option are mandatory for short options too.
  -r, --read
      view the previously running tomcat information.
  -l xxx, --list xxx
      View the listening port of the Tomcat project.
  -p NUMBER, --port NUMBER
        View the port information that the item listens on just now.
  -v, --version
      view the version
  --clear
      when the number of row is greater than 100, then clean-up, and leaving 30 lines.
  -h, --help
      display this help and exit
  -f, --file
      find temporary file.

E-mail bug reports to: <773805731@qq.com>


  查看tomcat的部署信息(实时信息):

[view@payqa1 ~]$ fint
1.PID=11131 USER=work TOMCAT=/opt/work/zcw_app_10380 WEBAPPS=/opt/work/release/app.
2.PID=13374 USER=work TOMCAT=/opt/work/zcw_back_10180 WEBAPPS=/opt/work/release/back.
# 系统中运行了两个java进程(tomcat)

  查看保存的信息

[view@payqa1 ~]$ fint -r
...
2017-10-22 21:36:46
1.PID=11131 USER=work TOMCAT=/opt/work/zcw_app_10380 WEBAPPS=/opt/work/release/app.
2.PID=13374 USER=work TOMCAT=/opt/work/zcw_back_10180 WEBAPPS=/opt/work/release/back.
...

  查看保存信息的文件位置

[view@payqa1 ~]$ fint -f
/tmp/.work/tmpFile4TomcatProcess


  脚本程序 fint

#!/bin/bash
# fint-2.8.5
# Find the tomcat container directory in the process running java environment.
# Output:
# 1 >>> PID=1921    Tomcat is: /data/tomcat/apache-tomcat-6.0.48    webapps is: 
# 1     2                          3                                                4
# Reference Information:
# Refer to the results of program running:
# PID=10170    Tomcat is : /home/zcw_task/tomcat7_task
# PID=9003    Tomcat is : /home/zhaocai/tomcat-p2p
#
# Script statement reference:
# /home/zcw_task/tomcat7_task/conf/server.xml
# cat /home/zcw_task/tomcat7_task/conf/server.xml | grep -v ! | grep docBase | sed 's/[[:space:]]/\n/g' | grep docBase

# Variables
# According to the CMD to obtain PID information.
#    CMD        The CMD is running the "java" process, used to filter the pid (process number).
#    PID        The PID is the "java" process of the pid (process number).
# TOMCAT_HOMEDIR    Tomcat's home directory.
# TOMCAT_CONFFILE    Tomcat's configuration file directory.
# TOMCAT_WEBAPPS    Web application directory.
#    K        A counter variable that is an integer. The number of java processes running in memory.
CMD="java"
PID=""
TOMCAT_HOMEDIR=""
TOMCAT_CONFFILE=""
TOMCAT_WEBAPPS=""
#     K        Indicates the number of processes.
#    tmp_Var        Save the required information
#    fin_Var        Same as upper.
#    TMPFILE        Save the temporary file of the message.
declare -i K=0
tmp_Var=""
fin_Var=""
USERD="$(whoami)"
TMPDIR="/tmp/.${USERD}"
TMPFILE="${TMPDIR}/tmpFile4TomcatProcess"
#    NUMROWDISPLAY    The number of rows displayed when viewing information.
#    NUMROWKEEP    The number of rows reserved when cleaning temporary files.
#    NUMROWBEGINDEL    The number of critical lines when cleaning temporary files.
# The next two variables are an integer multiple of number 3.
declare -i NUMROWDISPLAY=24
declare -i NUMROWKEEP=30
declare -i NUMROWBEGINDEL=100

# Command
# Checking command
# Judeg file in /tmp.
chk_TMPFileD () {
    [ -f $TMPDIR ] && rm -fv $TMPDIR
    [ ! -d $TMPDIR ] && mkdir -v $TMPDIR && ([ $? = 0 ] || exit 33)
}

# Judge parameters for options.
# Usage: "chk_pv2 $2".
chk_pv2 () {
    if [ -z $1 ]; then
        echo "Option is missing parameter."
        exit 1
    fi
}

# Find pid.
# Gets pid
# Gets the process ID of the CMD(java) process.
find_JavaPID () {
    PID=$(netstat -ltp 2>/dev/null | grep $CMD | awk '{print $7}' | cut -d/ -f1 | sort | uniq 2> /dev/null)
    # If can not get the PID, exit and an error message is displayed.
    if [ -z "$PID" ]; then
        echo "The process does not run the $CMD program."
        exit 1
    fi
    #PID="\t"$PID
}
# Processes the acquired PID.
# The main function is used to get the results.
find_tomsINFO () {
    find_JavaPID
    # Generate the information that needs to be output.
    # Information: $TOMCAT_HOMEDIR, $TOMCAT_WEBAPPS.
    # sn, pid, $TOMCAT_HOMEDIR, $TOMCAT_WEBAPPS.
    for i in $(echo $PID); do
            # The capital letter k indicates the serial number of the display information line.
            let K+=1
        # 1:Serial number
        tmp_Var="$tmp_Var"$(printf "%s." $K)
        # 2:show pid
        tmp_Var="$tmp_Var""$(printf "PID=%s " "$i")"
        # 3:pid's USER
        tmp_Var="$tmp_Var""$(printf "USER=%s TOMCAT=" "$(ps -p $i -o user | sed '1d')")"
        # 4:show tomcat home
            # From process information to the directory where the process runs.
            # Use the "ps" command to get the final result of the variable.
            TOMCAT_HOMEDIR=$(ps -p $i -o cmd | tail -1 | sed 's/[[:space:]]/\n/g' | grep "catalina\.home" | cut -d= -f2)
        tmp_Var="$tmp_Var"$(printf "%s WEBAPPS=" "$TOMCAT_HOMEDIR")
        # 5:show webapps
            # Get the tomcat configuration file.
            TOMCAT_CONFFILE="${TOMCAT_HOMEDIR}/conf/server.xml"
            # Get the set project location from the configure file.
            TOMCAT_WEBAPPS=$(cat $TOMCAT_CONFFILE | grep -v ! | grep docBase | sed 's/[[:space:]]/\n/g' | grep docBase | cut -d= -f2 | sed 's/\"//g')
        tmp_Var="$tmp_Var"$(printf "%s." $TOMCAT_WEBAPPS)"\n"
    done
    # return: 
    tmp_Var=${tmp_Var%/n}
}

# The function for "case".
run_it () {
    # Call the function to get the variable information.
    # Information: function is "find_tomsINFO", variable are "$TOMCAT_HOMEDIR", "$TOMCAT_WEBAPPS".
    find_tomsINFO
    # Timestamp information added to the log. 
    fin_Var=$(date +%F" "%T)"\n""$tmp_Var"
    # Filter out blank lines with a horizontal tab.
    # tmp_Var=$(echo -e $fin_Var | grep -v "^[[:blank:]]$")
    # echo "test: $fin_Var"
    # Output to a variable.
    echo -e $fin_Var | grep -v "^[[:blank:]]$" >> $TMPFILE
    # echo -e $tmp_Var >> $TMPFILE
    
    #echo "test: run_it()"
    #echo "$tmp_Var"
    # Output to the screen.
    #tmp_Var=" "$tmp_Var
    #echo -e $tmp_Var | grep -v "^[[:blank:]]$" | sed 's/[[:blank:]]//'
    echo -e $tmp_Var
    # echo -e $tmp_Var
}

read_it () {
    tail -$NUMROWDISPLAY $TMPFILE 2> /dev/null
}

list_port () {
    local FINDITEM=$1
    local PID=""
    #echo $FINDITEM
    PID=$($0 | grep "$FINDITEM" | cut -d" " -f1 | cut -d"=" -f2)
    #echo pid=$PID
    if [ -z "$PID" ]; then
        echo "Error, item does not exist."    
    else
        list | grep "$PID"
    fi
}

list_port2 () {
    list | grep "$1"
}

display_version () {
    sed -n '/fint/p' $0 | sed -n '1p' | cut -d"#" -f2
}

clear_TMPFILE () {
    # Get the number of lines in temporary file $TMPFILE.
    local -i j=$(cat $TMPFILE | wc -l)
    # If the number of rows calculated is greater than 100, the number of
    # lines of the temporary file is cleared to the specified number.
    if [ $j -gt $NUMROWBEGINDEL ]; then
        let j=j-${NUMROWKEEP}
        # The number of rows deleted this time.
        echo "The number of rows in $TMPFILE will be deleted this time: $j."
        echo -ne "3\a\b"; sleep 1; echo -ne "2\a\b"; sleep 1; echo -ne "1\a\b"; sleep 1; echo -ne "0\b"; echo -n " "
        sed -i '1,'${j}'d' $TMPFILE
    else
        echo "The number of rows is small and cleanup file is ignored."
    fi
}

display_tmpFile () {
    echo $TMPFILE
}

display_help () {
    cat << EOF
Usage: findTom [OPTION]
View the tomcat information.
View the home directory of the tomcat program running on the system, and
the project path. 

Mandatory arguments to long option are mandatory for short options too.
  -r, --read
      view the previously running tomcat information.
  -l xxx, --list xxx
      View the listening port of the Tomcat project.
  -p NUMBER, --port NUMBER
        View the port information that the item listens on just now.
  -v, --version
      view the version
  --clear
      when the number of row is greater than 100, then clean-up, and leaving 30 lines.
  -h, --help
      display this help and exit
  -f, --file
      find temporary file.

E-mail bug reports to: <773805731@qq.com>
EOF
}

chk_TMPFileD
# Fatal blow.
case "$1" in
    "")
        run_it
        ;;
    -r|--read)
        read_it
        ;;
    -l|--list)
        chk_pv2 $2
        list_port $2
        ;;
    -p|--port)
        chk_pv2 $2
        list_port2 $2
        ;;
    -v|--version)
        display_version
        ;;
    --clear)
        clear_TMPFILE
        ;;
    -f|--file)
        display_tmpFile
        ;;
    -h|--help)
        display_help
        ;;
    *)
        echo "findTom -h"
        display_help
        ;;
esac
# Shell script over.