bash 在 Centos 上启动时启动 Java 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22925608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launch Java application at startup on Centos
提问by Ferite
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. But the java application does not start. I have:
我将“/bin/sh /home/someuser/Desktop/Dist/lanzar.sh”行附加到/etc/rc.d/rc.local。但是java应用程序没有启动。我有:
- Granted 755 rights to the /etc/rc.d/rc.local file
- Write the content of the "lanzar.sh" into /etc/rc.d/rc.local. Separated with semicolon, and in different lines.
- Changing "lanzar.sh" of location.
- Other things, taken from other threads that did not work for me.
- 授予对 /etc/rc.d/rc.local 文件的 755 权限
- 将“lanzar.sh”的内容写入/etc/rc.d/rc.local。用分号分隔,并在不同的行中。
- 更改位置的“lanzar.sh”。
- 其他东西,取自其他对我不起作用的线程。
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.
注意:我知道以前有人问过类似的问题,但是在测试了我通过谷歌搜索找到的许多答案都没有成功之后,我不得不自己问这个问题。
回答by David W.
I highly recommend that you explore the /etc/init.d
directory of your server and the /etc/rc3.d
directory. See how the names of the files in /etc/rc3.d
are symbolically linked to the names in the /etc/init.d
directory. Notice how the files in /etc/rc3.d
all start with Sxx
or Kxxwhere
xx is a number between 00
to 99
.
我强烈建议您浏览/etc/init.d
服务器的/etc/rc3.d
目录和目录。查看 中文件的名称如何/etc/rc3.d
符号链接到/etc/init.d
目录中的名称。请注意,在文件中如何/etc/rc3.d
都开始用Sxx
或KXX where
XX之间的数字00
来99
。
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 Statesof the server. (Yes, I'm linking to an SCO Unix page, but they were all pretty similar).
在标准的 Unix 和 Linux 中,启动脚本通常存储在/etc/init.d
并链接到代表服务器初始状态的/etc/rcX.d
目录中。(是的,我正在链接到 SCO Unix 页面,但它们都非常相似)。X
Note that Init State 3 is running in multi-user mode and that all the daemons are started. This is why I am telling you to look in /etc/rc3.d
.
请注意,Init State 3 正在多用户模式下运行,并且所有守护程序都已启动。这就是为什么我告诉你往里看/etc/rc3.d
。
When the server enters that init state, it runs all of the script starting with S
in alphabetical order. It runs each script with the parameter start
after it. So, S01xxxx
starts before S03xxx
which starts before S99xxxxx
.
当服务器进入该init 状态时,它会S
按字母顺序运行所有脚本。它运行每个脚本,并在start
其后运行参数。所以,S01xxxx
在之前S03xxx
开始在之前开始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.
当服务器退出该init 状态时,它会运行所有K
以字母顺序开头的脚本,并将stop
参数传递给它们。
Now, Centos, Redhat, and Fedora setup handles a lot of this for you. 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.
现在,Centos、Redhat 和 Fedora 安装程序为您处理了很多事情。您指定您依赖的服务,它会计算出启动和关闭顺序。但是,没有什么可以阻止您修改启动脚本并创建自己的链接。
By the way, speaking about Java programs that startup and shutdown... Jenkinsis a Java program that's started in a very similar way as your program. Here's the /etc/init.d
script I got off of Jenkins website:
顺便说一下,关于启动和关闭的 Java 程序…… Jenkins是一个 Java 程序,它的启动方式与您的程序非常相似。这是/etc/init.d
我从 Jenkins 网站上获取的脚本:
#!/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 }'` || 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 "" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo $"Usage: ##代码## {start|stop|restart|status}"
exit 1
esac
exit $RETVAL
It'll give you something to work with.
它会给你一些工作。