#!/bin/bash JAVA_OPTIONS="-Xmx1024m -Xms126m -Duser.timezone=GMT+08" JARPATH=`pwd` declare -a JARNAME function findJAR() { JARNAME=($(ls -l $JARPATH | grep jar$ | awk '{print $NF}')) if [[ ${#JARNAME[@]} > 1 ]] then echo "[${JARNAME[@]}] too many jar files!!! I don't know startup which one." exit 1 elif [[ ${#JARNAME[@]} == 0 ]] then echo "this is no jar file in $JARPATH" exit 1 else echo "---------" fi } function rename() { if [[ ${#JARNAME[@]} != 1 ]] then echo "[${JARNAME[@]}] too many jar files!!! I don't know which one should be renamed." exit 1 else timestamp=`date "+%Y%m%d%H%M%S"` mv "${JARNAME[0]}" "${JARNAME[0]}${timestamp}" echo "${JARNAME[0]} has been renamed to ${JARNAME[0]}${timestamp}" fi } function start() { server_pid=`ps -ef | grep java | grep ${JARNAME[0]} | awk '{print $2}'` if [ "${server_pid}" != "" ] then echo -e "${JARNAME[0]} is alived with pid ${server_pid}.\nabort start action" exit 1 fi nohup java ${JAVA_OPTIONS} -jar ${JARNAME[0]} > /dev/null 2>&1 & server_pid=`ps -ef | grep java | grep ${JARNAME[0]} | awk '{print $2}'` echo "${JARNAME[0]} is alived with pid ${server_pid}." } function status() { server_pid=`ps -ef | grep java | grep ${JARNAME[0]} | awk '{print $2}'` if [ "${server_pid}" != "" ] then echo -e "${JARNAME[0]} is alived with pid ${server_pid}." else echo "${JARNAME[0]} isn't alived." fi } function stop() { server_pid=`ps -ef | grep java | grep ${JARNAME[0]} | awk '{print $2}'` if [ "$server_pid" != "" ] then echo "${JARNAME[0]} is alived with pid ${server_pid}." kill -9 $server_pid echo "has stopped ${JARNAME[0]}" else echo "${JARNAME[0]} isn't alived." fi } function usage(){ echo -e "Usage:sh deploy.sh COMMAND \nCOMMAND OPTIONS:\nstart:start the server\nstop:stop the server\nrestart:restart the server\nstatus:show the server pid.\nrename:rename the jar with timestamp.\n" } findJAR case "$1" in "start") start ;; "stop") stop ;; "restart") stop start ;; "status") status ;; "rename") rename ;; *) usage ;; esac
标签:grep,jar,pid,server,启停,linux,echo,JARNAME From: https://www.cnblogs.com/zenghaoxhx/p/16623423.html