简体   繁体   English

在Centos上启动时启动Java应用程序

[英]Launch Java application at startup on Centos

I need to launch a Java application on Centos (5.9) startup. 我需要在Centos(5.9)启动时启动Java应用程序。

I am trying to start a simple script (named "lanzar.sh") on Centos at boot time: 我试图在启动时在Centos上启动一个简单的脚本(名为“lanzar.sh”):

#!/bin/sh
cd /home/someuser/Desktop/Dist
java -jar SomeApp.jar

I append the line "/bin/sh /home/someuser/Desktop/Dist/lanzar.sh" to /etc/rc.d/rc.local. 我将“/ bin / sh /home/someuser/Desktop/Dist/lanzar.sh”行附加到/etc/rc.d/rc.local。 But the java application does not start. 但java应用程序无法启动。 I have: 我有:

  • Granted 755 rights to the /etc/rc.d/rc.local file 授予/etc/rc.d/rc.local文件的755权限
  • Write the content of the "lanzar.sh" into /etc/rc.d/rc.local. 将“lanzar.sh”的内容写入/etc/rc.d/rc.local。 Separated with semicolon, and in different lines. 用分号分隔,并用不同的行分隔。
  • Changing "lanzar.sh" of location. 更改位置的“lanzar.sh”。
  • Other things, taken from other threads that did not work for me. 其他的东西,取自其他对我不起作用的线程。

My rc.loca looks like: 我的rc.loca看起来像:

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.
#
#Some comment

#Some comment

#Some comment
touch /var/lock/subsys/local
/bin/sh /home/fernando/Desktop/Dist/lanzar.sh

Note: I know similar questions have been asked before, but after testing many of the answers that I have found by googling with no success, I had to ask this myself. 注意:我之前已经知道类似的问题,但在测试了我通过谷歌搜索找到的许多答案后没有成功,我不得不自己问这个问题。

I highly recommend that you explore the /etc/init.d directory of your server and the /etc/rc3.d directory. 我强烈建议您浏览服务器的/etc/init.d目录和/etc/rc3.d目录。 See how the names of the files in /etc/rc3.d are symbolically linked to the names in the /etc/init.d directory. 了解/etc/rc3.d中文件的名称如何与/etc/init.d目录中的名称符号链接。 Notice how the files in /etc/rc3.d all start with Sxx or Kxx where xx is a number between 00 to 99 . 注意/etc/rc3.d的文件是如何以Sxx或Kxx开头的where xx是介于0099之间的数字。

What I am about to tell you is officially all wrong. 我要告诉你的是正式的错误。 These startup scripts are way more complicated today that what I describe, but it's a basic outline of what's going on. 这些启动脚本今天比我描述的更复杂,但它是正在发生的事情的基本轮廓。

In standard Unix and Linux, startup scripts were normally stored in /etc/init.d and then linked to the /etc/rcX.d directory where X stood for what was called the Init States of the server. 在标准的Unix和Linux中,启动脚本通常存储在/etc/init.d ,然后链接到/etc/rcX.d目录,其中X代表所谓的服务器的Init状态 (Yes, I'm linking to an SCO Unix page, but they were all pretty similar). (是的,我正在链接到SCO Unix页面,但它们都非常相似)。

Note that Init State 3 is running in multi-user mode and that all the daemons are started. 请注意,Init State 3以多用户模式运行,并且所有后台进程都已启动。 This is why I am telling you to look in /etc/rc3.d . 这就是我告诉你查看/etc/rc3.d

When the server enters that init state , it runs all of the script starting with S in alphabetical order. 当服务器进入该初始状态时 ,它按字母顺序运行以S开头的所有脚本。 It runs each script with the parameter start after it. 它运行每个脚本后面的参数start So, S01xxxx starts before S03xxx which starts before S99xxxxx . 因此, S01xxxxS03xxx之前启动, S01xxxxS03xxx之前S99xxxxx

When the server exits that init state , it runs all of the scripts that start with K in alphabetical order, and passes the stop parameter to them. 当服务器退出该初始状态时 ,它按字母顺序运行以K开头的所有脚本,并将stop参数传递给它们。

Now, Centos, Redhat, and Fedora setup handles a lot of this for you. 现在,Centos,Redhat和Fedora设置为您处理了很多这样的事情。 You specify which service you depend upon, and it figures out startup and shutdown order. 您可以指定依赖的服务,并指出启动和关闭顺序。 However, nothing is preventing you from munging a startup script and creating your own links. 但是,没有任何东西阻止您修改启动脚本并创建自己的链接。

By the way, speaking about Java programs that startup and shutdown... Jenkins is a Java program that's started in a very similar way as your program. 顺便说一下,谈论启动和关闭的Java程序...... Jenkins是一个Java程序,它以与程序非常相似的方式启动。 Here's the /etc/init.d script I got off of Jenkins website: 这是我从Jenkins网站下载的/etc/init.d脚本:

#!/bin/bash
#
# Startup script for Jenkins
#
# chkconfig: - 84 16
# description: Jenkins CI server

# Source function library.
. /etc/rc.d/init.d/functions
[ -z "$JAVA_HOME" -a -x /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh

JENKINS_HOME=/var/jenkins
WAR="$JENKINS_HOME/jenkins.war"
LOG="/var/log/jenkins.log"
LOCK="/var/lock/subsys/jenkins"
export JENKINS_HOME

RETVAL=0

pid_of_jenkins() {
    pgrep -f "java.*jenkins"
}

start() {
    [ -e "$LOG" ] && cnt=`wc -l "$LOG" | awk '{ print $1 }'` || cnt=1

    echo -n $"Starting jenkins: "

    cd "$JENKINS_HOME"
    nohup java -jar "$WAR" --httpPort=-1 --ajp13Port=8010 --prefix=/jenkins >> "$LOG" 2>&1 &

    while { pid_of_jenkins > /dev/null ; } &&
        ! { tail +$cnt "$LOG" | grep -q 'Winstone Servlet Engine .* running' ; } ; do
        sleep 1
    done

    pid_of_jenkins > /dev/null
    RETVAL=$?
    [ $RETVAL = 0 ] && success $"$STRING" || failure $"$STRING"
    echo

    [ $RETVAL = 0 ] && touch "$LOCK"
}

stop() {
    echo -n "Stopping jenkins: "

    pid=`pid_of_jenkins`
    [ -n "$pid" ] && kill $pid
    RETVAL=$?
    cnt=10
    while [ $RETVAL = 0 -a $cnt -gt 0 ] &&
        { pid_of_jenkins > /dev/null ; } ; do
            sleep 1
            ((cnt--))
    done

    [ $RETVAL = 0 ] && rm -f "$LOCK"
    [ $RETVAL = 0 ] && success $"$STRING" || failure $"$STRING"
    echo
}

status() {
    pid=`pid_of_jenkins`
    if [ -n "$pid" ]; then
        echo "jenkins (pid $pid) is running..."
        return 0
    fi
    if [ -f "$LOCK" ]; then
        echo $"${base} dead but subsys locked"
        return 2
    fi
    echo "jenkins is stopped"
    return 3
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit $RETVAL

It'll give you something to work with. 它会给你一些工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM