Linux start-up script for java application

前端 未结 6 932
醉梦人生
醉梦人生 2021-01-07 11:31

I have Jar file to run in Linux using this command:

java -jar SyslogAgent_01.jar 192.168.2.154 1998 D:/apachelog.log ApacheLog 13

Can anyon

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-07 11:47

    I would start with this template for a startup script rename the SCRIPT_HOME to proper path and call this file without any extensions, then in SSH run this command.

    chkconfig –add javaserver
    

    Note the javaserver in the chkconfig is how you called the file below, (no extensions or it wont work).

    #!/bin/bash
    #
    # javaserver: Startup script for Any Server Application.
    #
    # chkconfig: 35 80 05
    # description: Startup script for Any Server Application.
    
    SCRIPT_HOME=/var/java_server;
    export SCRIPT_HOME
    
    start() {
            echo -n "Starting Java Server: "
            $SCRIPT_HOME/run.sh start
            sleep 2
            echo "done"
    }
    
    stop() {
            echo -n "Stopping Java Server: "
            $SCRIPT_HOME/run.sh stop
            echo "done"
    }
    
    # See how we were called.
    case "$1" in
            start)
                    start
                    ;;
            stop)
                    stop
                    ;;
            restart)
                    stop
                    start
                    ;;
            *)
                    echo $"Usage: javaserver {start|stop|restart}"
                    exit
    esac
    

    Now here is the script the run.sh (this can be used instead of the template but I find it easier to make the template script small and unchangeable link to my main script so I never have to touch it again.

    The script below is one of a very few scripts I found that actually can restart a java program without turning off all the java programs you currently have running, this one only targets the program it opened in the first place so it will never make any mistakes, never had it fail on me, run on user root no problem.
    Runs your program in the background forever too (no need to keep SSH opened).

    This is the run.sh

    #!/bin/bash
    #
    # chkconfig: 345 99 05 
    # description: Java deamon script
    #
    # A non-SUSE Linux start/stop script for Java daemons.
    #
    # Set this to your Java installation
    JAVA_HOME=/usr/bin/
    
    scriptFile=$(readlink -fn $(type -p $0))                                        # the absolute, dereferenced path of this script file
    scriptDir=$(dirname $scriptFile)                                                    # absolute path of the script directory
    serviceNameLo="javaserver"                                                       # service name with the first letter in lowercase
    serviceName="JavaServer"                                                           # service name
    serviceUser="root"                                                                           # OS user name for the service
    serviceGroup="root"                                                                         # OS group name for the service
    applDir="/var/java_server"                                       # home directory of the service application
    serviceUserHome="/home/$serviceUser"                                      # home directory of the service user
    serviceLogFile="/var/log/$serviceNameLo.log"                            # log file for StdOut/StdErr
    maxShutdownTime=15                                                                     # maximum number of seconds to wait for the daemon to terminate normally
    pidFile="/var/run/$serviceNameLo.pid"                                        # name of PID file (PID = process ID number)
    javaCommand="java"                                                                       # name of the Java launcher without the path
    javaArgs="MyJavaAppClass"                                                                  # arguments for Java launcher
    javaCommandLine="$JAVA_HOME$javaCommand $javaArgs"   # command line to start the Java service application
    javaCommandLineKeyword="MyJavaAppClass"                                   # a keyword that occurs on the commandline, used to detect an already running service process and to distinguish it from others
    
    # Makes the file $1 writable by the group $serviceGroup.
    function makeFileWritable {
       local filename="$1"
       touch $filename || return 1
       chgrp $serviceGroup $filename || return 1
       chmod g+w $filename || return 1
       return 0; }
    
    # Returns 0 if the process with PID $1 is running.
    function checkProcessIsRunning {
       local pid="$1"
       if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
       if [ ! -e /proc/$pid ]; then return 1; fi
       return 0; }
    
    # Returns 0 if the process with PID $1 is our Java service process.
    function checkProcessIsOurService {
       local pid="$1"
       if [ "$(ps -p $pid --no-headers -o comm)" != "$javaCommand" ]; then return 1; fi
       grep -q --binary -F "$javaCommandLineKeyword" /proc/$pid/cmdline
       if [ $? -ne 0 ]; then return 1; fi
       return 0; }
    
    # Returns 0 when the service is running and sets the variable $pid to the PID.
    function getServicePID {
       if [ ! -f $pidFile ]; then return 1; fi
       pid="$(<$pidFile)"
       checkProcessIsRunning $pid || return 1
       checkProcessIsOurService $pid || return 1
       return 0; }
    
    function startServiceProcess {
       cd $applDir || return 1
       rm -f $pidFile
       makeFileWritable $pidFile || return 1
       makeFileWritable $serviceLogFile || return 1
       cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
       # Don't forget to add -H so the HOME environment variable will be set correctly.
       #sudo -u $serviceUser -H $SHELL -c "$cmd" || return 1
       su --session-command="$javaCommandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile" $serviceUser || return 1
       sleep 0.1
       pid="$(<$pidFile)"
       if checkProcessIsRunning $pid; then :; else
          echo -ne "\n$serviceName start failed, see logfile."
          return 1
       fi
       return 0; }
    
    function stopServiceProcess {
       kill $pid || return 1
       for ((i=0; i

提交回复
热议问题