所以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"工作,我在控制台中得到"管理员启动/运行"..没有创建日志,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文件的简化版本.
如果你想进一步调整这个,那么start-stop-daemon的手册页值得一看