General Script 2 for starting java programs in linux, linuxjava

Source: Internet
Author: User

General Script 2 for starting java programs in linux, linuxjava

1. start. sh


#! /Bin/bash

Cd 'dirname $0'
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 $2 }''
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 $0}' | tr "\ n "":"'


JAVA_OPTS = "-Djava. awt. headless = true -Djava.net. preferIPv4Stack = true"
JAVA_DEBUG_OPTS = ""
If ["$1" = "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 ["$1" = "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 = 128 m-Xss256k-XX: + DisableExplicitGC-XX: + response-XX: + UseCMSCompactAtFullCollection-XX: LargePageSizeInBytes = 128 m-XX: + UseFastAccessorMethods-XX: + usecmsinitiatingoccupancycyonly-XX: CMSInitiatingOccupancyFraction = 70"
Else
JAVA_MEM_OPTS = "-server-Xmx1024m-Xms1024m-Xmn400m-XX: PermSize = 128 m-Xss256k-XX: + DisableExplicitGC-XX: + response-XX: + UseCMSCompactAtFullCollection-XX: LargePageSizeInBytes = 128 m-XX: + UseFastAccessorMethods-XX: + usecmsinitiatingoccupancycyonly-XX: CMSInitiatingOccupancyFraction = 70"
Fi
Else
JAVA_MEM_OPTS = "-server-Xmx1024m-Xms1024m-XX: PermSize = 128 m-XX: Invalid vorratio = 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 $2}' | 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 $2 }''
Echo "PID: $ PIDS"

# Echo "STDOUT: $ STDOUT_FILE"


Ii. stop. sh


#! /Bin/bash
Cd 'dirname $0'
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 $2 }''
If [-z "$ PIDS"]; then
Echo "ERROR: The $ SERVER_NAME does not started! "
Exit 1
Fi


If ["$1 "! = "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"



3. restart. sh


#! /Bin/bash
Cd 'dirname $0'
./Stop. sh
./Start. sh


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.