具有嵌入式 init.d 脚本的 Spring Boot 应用程序未在重新启动时启动

Posted

技术标签:

【中文标题】具有嵌入式 init.d 脚本的 Spring Boot 应用程序未在重新启动时启动【英文标题】:Spring Boot app with embedded init.d script not starting on reboot 【发布时间】:2016-10-12 00:33:06 【问题描述】:

Spring Boot 有一个方便的功能,如果您配置 maven 插件,它会将 init.d 启动脚本嵌入到可执行 jar 中: http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-install

所以我正在使用上述方法在 Centos 6.6 中“安装”一个 Spring Boot 应用程序(可执行的 fat jar)作为服务。

到目前为止一切顺利。我创建一个指向我的 jar 的链接并设置权限:

sudo ln -s  /path/to/myapp.jar  /etc/init.d/my-service
sudo chmod 0755 /etc/init.d/my-service

并且我可以成功启动应用程序:

sudo service my-service start

但是,我希望应用在启动时启动,所以我使用 chkconfig 实用程序:

sudo chkconfig --add my-service
sudo chkconfig my-service on

这些命令没有错误,但是当我重新启动 Centos 时,服务不会自动启动。 运行:

sudo service my-service status

显示:

Not running

运行:

chkconfig --list my-service

显示:

my-service      0:off   1:off   2:on    3:on    4:on    5:on    6:off

一切看起来都不错,但还没有开始。此时我可以使用“sudo service my-service start”手动启动该服务,它会重新启动,但它不会在启动时自动启动。

有什么想法吗? 谢谢

【问题讨论】:

【参考方案1】:

问题解决了。原来是路径问题和我的错。应用程序所在的路径是一个在引导时不可用的挂载目录。

【讨论】:

【参考方案2】:

我有类似的问题,当我运行service myapp start时收到以下错误消息

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springframework/boot/loader/JarLauncher : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: org.springframework.boot.loader.JarLauncher. Program will exit.

但是我可以通过直接运行 /etc/init.d/app start 使其工作。

最后我发现问题是默认 jdk 是 1.6,尽管当前用户的 java 是 1.8。然后我改变了符号,它起作用了。

【讨论】:

以上是关于具有嵌入式 init.d 脚本的 Spring Boot 应用程序未在重新启动时启动的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 内嵌启动脚本配置(Customizing the startup script)

linux自动启动程序用ctrl+c无法终止

Erlang:守护进程“init.d”脚本无法启动

为啥许多 init.d 脚本以“exit $?”结尾?

linux init.d启动停止脚本

linux shell脚本中如何引入当前的系统环境变量