Generic script for starting Java programs under Linux 2

Source: Internet
Author: User

First, start.sh


#!/bin/bash

CD ' DirName $ '
Bin_dir= ' pwd '
Cd..
Deploy_dir= ' pwd '
conf_dir= $DEPLOY _dir/conf


Server_name= ' sed '/server.name/!d;s/.*=//' conf/server.properties | Tr-d ' \ r '
server_port= ' sed '/server.netty.port/!d;s/.*=//' conf/server.properties | Tr-d ' \ r '


If [-Z "$SERVER _name"]; Then
Server_name= ' hostname '
Fi


Pids= ' PS--no-heading-c java-f--width 1000 | grep "$CONF _dir" |awk ' {print $} '
If [-N "$PIDS"]; Then
echo "Error:the $SERVER _name already started!"
echo "PID: $PIDS"
Exit 1
Fi


If [-N "$SERVER _port"]; Then
Server_port_count= ' Netstat-tln | grep $SERVER _port | Wc-l '
If [$SERVER _port_count-gt 0]; Then
echo "Error:the $SERVER _name Port $SERVER _port already used!"
Exit 1
Fi
Fi


Logs_dir= ""
logs_dir= $DEPLOY _dir/logs


#if [!-d $LOGS _dir]; Then
# mkdir $LOGS _dir
#fi
stdout_file= $LOGS _dir/stdout.log


lib_dir= $DEPLOY _dir/lib
Lib_jars= ' Find $LIB _dir|grep. Jar|awk ' {print $} ' |tr ' \ n ' "": '


Java_opts= "-djava.awt.headless=true-djava.net.preferipv4stack=true"
Java_debug_opts= ""
If ["$" = "Debug"]; Then
Java_debug_opts= "-xdebug-xnoagent-djava.compiler=none-xrunjdwp:transport=dt_socket,address=8000,server=y, Suspend=n "
Fi
Java_jmx_opts= ""
If ["$" = "JMX"]; Then
Java_jmx_opts= "-dcom.sun.management.jmxremote.port=1099-dcom.sun.management.jmxremote.ssl=false- Dcom.sun.management.jmxremote.authenticate=false "
Fi
Java_mem_opts= ""
bits= ' File $JAVA _home/bin/java | grep 64-bit '
If [-N "$BITS"]; Then
Let memtotal= ' Cat/proc/meminfo |grep memtotal|awk ' {printf '%d ', $2/1024} '
If [$memTotal-gt 2500];then
Java_mem_opts= "-server-xmx1024m-xms1024m-xmn400m-xx:permsize=128m-xss256k-xx:+disableexplicitgc-xx:+ useconcmarksweepgc-xx:+cmsparallelremarkenabled-xx:+usecmscompactatfullcollection-xx:largepagesizeinbytes=128m -xx:+usefastaccessormethods-xx:+usecmsinitiatingoccupancyonly-xx:cmsinitiatingoccupancyfraction=70 "
Else
Java_mem_opts= "-server-xmx1024m-xms1024m-xmn400m-xx:permsize=128m-xss256k-xx:+disableexplicitgc-xx:+ useconcmarksweepgc-xx:+cmsparallelremarkenabled-xx:+usecmscompactatfullcollection-xx:largepagesizeinbytes=128m -xx:+usefastaccessormethods-xx:+usecmsinitiatingoccupancyonly-xx:cmsinitiatingoccupancyfraction=70 "
Fi
Else
Java_mem_opts= "-SERVER-XMX1024M-XMS1024M-XX:PERMSIZE=128M-XX:SURVIVORRATIO=2-XX:+USEPARALLELGC"
Fi


Echo-e "Starting the $SERVER _name ... \c"
Nohup java $JAVA _opts $JAVA _mem_opts $JAVA _debug_opts $JAVA _jmx_opts-classpath $CONF _dir: $LIB _jars Com.hzins.framework.server.ServerMain > $STDOUT _file 2>&1 &


Count=0
While [$COUNT-lt 1]; Do
Echo-e ". \c"
Sleep 1
# if [-N ' $SERVER _port "]; Then
#Count= ' echo status | NC 127.0.0.1 $SERVER _port-i 1 | Grep-c OK '
# Else
Count= ' PS--no-heading-c java-f--width 1000 | grep "$DEPLOY _dir" | awk ' {print $} ' | Wc-l '
# fi
If [$COUNT-gt 0]; Then
Break
Fi
Done
echo "ok!"
Pids= ' PS--no-heading-c java-f--width 1000 | grep "$DEPLOY _dir" | awk ' {print $} '
echo "PID: $PIDS"

#echo "STDOUT: $STDOUT _file"


Second, stop.sh


#!/bin/bash
CD ' DirName $ '
Bin_dir= ' pwd '
Cd..
Deploy_dir= ' pwd '
conf_dir= $DEPLOY _dir/conf


Server_name= ' sed '/server.name/!d;s/.*=//' conf/server.properties | Tr-d ' \ r '


If [-Z "$SERVER _name"]; Then
Server_name= ' hostname '
Fi


Pids= ' PS--no-heading-c java-f--width 1000 | grep "$CONF _dir" |awk ' {print $} '
If [-Z "$PIDS"]; Then
echo "Error:the $SERVER _name does not started!"
Exit 1
Fi


If ["$"! = "Skip"]; Then
$BIN _dir/dump.sh
Fi


Echo-e "Stopping the $SERVER _name ... \c"
For PID in $PIDS; Do
Kill $PID >/dev/null 2>&1
Done


Count=0
While [$COUNT-lt 1]; Do
Echo-e ". \c"
Sleep 1
Count=1
For PID in $PIDS; Do
Pid_exist= ' PS--no-heading-p $PID '
If [-N "$PID _exist"]; Then
Count=0
Break
Fi
Done
Done
echo "ok!"
echo "PID: $PIDS"



Third, restart.sh


#!/bin/bash
CD ' DirName $ '
./stop.sh
./start.sh


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Generic script for starting Java programs under Linux 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.