如何在伺服器重新啟動時自動啟動 java 服務

如何在伺服器重新啟動時自動啟動 java 服務

我通常在 CentOS 機器上運行我的 java 服務,nohup如下所示:

nohup java -jar myapp-0.0.1-SNAPSHOT.jar server my-configure.yml > outfile &

但是,如果伺服器重新啟動,該服務不會自行重新啟動。 CentOS中有沒有一種自動方式在啟動時啟動這個java服務?

答案1

將腳本複製到/etc/rc.d/rc.local,重新啟動,它將在啟動時運行。例如

[vagrant@localhost ~]$ cat /etc/rc.d/rc.local
#!/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.

touch /var/lock/subsys/local

sudo touch /tmp/helloworld

創造了/tmp/helloworld系統重新啟動後檔案:

[vagrant@localhost ~]$ ls /tmp/
helloworld 

30.3。在啟動時運行附加程序

The /etc/rc.d/rc.local script is executed by the init command at boot time or when changing runlevels. Adding commands to the bottom of this script is an easy way to perform necessary tasks like starting special services or initialize devices without writing complex initialization scripts in the /etc/rc.d/init.d/ directory and creating symbolic links.

The /etc/rc.serial script is used if serial ports must be setup at boot time. This script runs setserial commands to configure the system's serial ports. Refer to the setserial man page for more information. 

相關內容