作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以在 2 天后(是的,在服务器方面我是一个完全的菜鸟)试图让这个工作我放弃并转向 SO 寻求帮助:)
我想在启动时启动我的 java 应用程序,记录到日志文件。就是这样:)
start on runlevel [2345]
stop on runlevel [!2345]
#Respawn the process if it crashes
#If it respawns more than 10 times in 5 seconds stop
respawn
respawn limit 10 5
expect fork
script
cd /home/ubuntu/admin/
mvn spring-boot:run > /var/log/upstart/admin.log 2>&1
end script
运行“sudo start admin”有效,我在控制台中看到“admin start/running”。没有创建日志并且 java 应用程序没有启动。?
我错过了什么?
如何在 Ubuntu 上将 Java 作为服务运行?
最佳答案
我并不是要转移话题,但自 2010 年以来,我就在生产环境中的 Ubuntu 上部署了 Java 应用程序,但在 Upstart 上收效甚微。我使用 init.d 脚本和 start-stop-daemon。附带好处:它适用于更多发行版。
创建/etc/init.d/my-java-app:
#!/bin/sh
#
# my-java-app My Java App
#
# chkconfig: - 80 05
# description: Enable My Java Application
#
### BEGIN INIT INFO
# Provides: my-java-app
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: My Java Application
# Short-Description: Enable My Java Application
### END INIT INFO
DESC="my java app"
NAME=my-java-app
PIDFILE=/var/run/$NAME.pid
RUN_AS=ubuntu
WORK_DIR=/home/ubuntu/admin
DAEMON=/usr/bin/mvn
DAEMON_OPTS="spring-boot:run"
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
do_start() {
start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE \
--background \
--chuid $RUN_AS \
--chdir $WORK_DIR \
--exec $DAEMON -- $DAEMON_OPTS
}
do_stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE
if [ -e $PIDFILE ]
then rm $PIDFILE
fi
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
sleep 1
do_start
echo "."
;;
status)
status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
;;
*)
echo "usage: $NAME {start|stop|restart}"
exit 1
;;
esac
让它属于 root,让它可执行,并设置它在启动时运行:
sudo chown root:root /etc/init.d/my-java-app
sudo chmod 755 /etc/init.d/my-java-app
sudo update-rc.d my-java-app defaults
要启动您可以运行的服务:
sudo service my-java-app start
要停止您可以运行的服务:
sudo service my-java-app stop
这是基于 Ubuntu 包含的 /etc/init.d/skeleton 文件的简化版本。
man page for start-stop-daemon如果您想进一步调整它,值得一看。b
关于java - 如何在 Ubuntu 上将 Java 作为服务运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34443765/
我是一名优秀的程序员,十分优秀!