救命!
我在SpringBoot 1.3.6和Gradle中使用内置的启动脚本。哦,还有distZip任务来压缩内容。
不久前的某个时候,这一切工作得很好……然后我做了-我不知道是什么-把它搞砸了。
我已经在树莓派上安装了该软件包(基本上是解压缩了Zip),并检查了所有权和权限。一切都归我要以其身份运行该应用程序的用户(用户“appservice”组“pi”)所有,并确认文件的权限(如果有的话)太宽松(对于myapp / bin / myapp脚本为755,以及几乎所有其他内容)。
我在/etc/init.d中放置了一个指向〜appservice / myapp / bin / myapp的符号链接(symbolic link),并运行了update-rc.d myapp默认值使其进入系统。请注意,符号链接(symbolic link)本身是由root / root拥有的,但是我相信它应该是,不是吗?
我看到了两个相互关联的问题。
首先,无论我如何启动脚本(在init.d上启动或通过“sudo service myapp start”手动启动),它似乎都以root身份运行(特别是,该应用程序试图用来访问文件的路径显示为/ root / myapp / files而不是/ home / appservice / myapp / files)。
其次,该应用程序将崩溃...特别是我收到syslog消息“myapp.service启动操作超时。正在终止。”然后看起来像是有序关闭了该应用程序。哦,还有相关的...如果我使用“sudo service myapp start”启动,该命令将永远不会返回。哪个是新的...
第三,应用程序日志输出将转到/ var / log / syslog,而不是/var/log/myapp.log,这似乎与Spring Boot文档所说的相反。
我正在为此进行部署的最终回归测试,并且(著名的遗言)我最近没有更改任何东西... :)不,真的,与分发相关的最新更改是添加src / main / dist目录,并且此后仍然有效(以正确的用户身份启动时启动)。
我会发布启动脚本,但AFAIK是您使用springBoot {可执行= true}任务时Spring Boot提供的默认脚本。
这是我完整的build.gradle文件...我看不到任何毛病,但也许您会。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'application'
apply from: 'gradle/gradle/helpers.gradle'
mainClassName = 'app.Application'
if (!hasProperty('mainClass')) {
ext.mainClass = 'app.Application'
}
springBoot {
executable = true
}
repositories {
mavenCentral()
}
sourceSets {
main {
java { srcDir 'src/main/java' }
resources { srcDir '/src/main/resources' }
}
test {
java { srcDir 'src/test/java' }
resources { srcDir 'src/test/resources' }
}
}
project.ext {
applicationVersion = "0.1.5-alpha"
applicationRelease = isApplicationRelease()
applicationDate = new Date()
applicationRevision = getRevision()
applicationVersionSnapshot = (!applicationRelease) ? "+SNAPSHOT.${asUTC(applicationDate, 'yyMMddHHmm')}.git${applicationRevision}" : ""
applicationVersionFull = "${applicationVersion}${applicationVersionSnapshot}"
}
jar {
baseName = 'myapp'
version = '0.9.0'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile group: 'com.sun.mail', name: 'javax.mail', version: '1.5.2'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.6'
compile group: 'commons-cli', name:'commons-cli', version: '1.3.1'
compile group: 'org.json', name:'json', version: '20140107'
compile "commons-codec:commons-codec:1.10"
compile("org.springframework.boot:spring-boot-starter-hateoas")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-mail")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework:spring-web")
compile("org.springframework:spring-messaging")
compile("joda-time:joda-time:2.2")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("com.googlecode.json-simple:json-simple")
compile("org.jdom:jdom:2.0.0")
compile("org.hibernate:hibernate-validator")
compile("org.apache.tomcat.embed:tomcat-embed-el")
compile("org.apache.commons:commons-io:1.3.2")
compile("org.kamranzafar:jtar:2.3")
compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4")
compile("com.jcraft:jsch:0.1.53")
compile("javax.jmdns:jmdns:3.4.1")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.14'
}
最佳答案
我建议使用systemd
运行和管理您的应用程序。
这使得在特定用户下运行应用程序非常容易。
为此,请按照下列步骤操作:
首先,使用以下内容创建服务定义文件/etc/systemd/system/myapp.service
:
[Unit]
Description=My App
[Service]
User=nobody
# The configuration file application.properties should be here:
WorkingDirectory=/home/appservice/myapp/files
ExecStart=/usr/bin/java -Xmx256m -jar myapp.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
然后,将新的服务文件通知给
systemd
:systemctl daemon-reload
并启用它,使其在启动时运行:
systemctl enable myapp.service
最后,您可以使用以下命令来启动/停止新服务:
systemctl start myapp
systemctl stop myapp