Java Spring引导可执行jar,init.d以root而不是用户身份启动

Java Spring引导可执行jar,init.d以root而不是用户身份启动,java,gradle,spring-boot,executable-jar,init.d,Java,Gradle,Spring Boot,Executable Jar,Init.d,救命啊 我正在使用SpringBoot 1.3.6和Gradle的内置启动脚本。哦,还有把东西拉起来的distZip任务 在不久前的某个时刻,这一切都运行得很好。。。然后我做了--我不知道是什么--把事情搞砸了 我已经在我的raspberry Pi上安装了这个包(基本上是解压缩的),并检查了所有权和权限。所有内容都由我希望以用户身份运行应用程序的用户(用户“appservice”组“pi”)所有,并确认文件的权限过于宽松(对于myapp/bin/myapp脚本和其他几乎所有内容,权限为755)

救命啊

我正在使用SpringBoot 1.3.6和Gradle的内置启动脚本。哦,还有把东西拉起来的distZip任务

在不久前的某个时刻,这一切都运行得很好。。。然后我做了--我不知道是什么--把事情搞砸了

我已经在我的raspberry Pi上安装了这个包(基本上是解压缩的),并检查了所有权和权限。所有内容都由我希望以用户身份运行应用程序的用户(用户“appservice”组“pi”)所有,并确认文件的权限过于宽松(对于myapp/bin/myapp脚本和其他几乎所有内容,权限为755)

我在/etc/init.d中放置了一个指向~appservice/myapp/bin/myapp的符号链接,并运行了update rc.d myapp defaults将其放入系统。注意符号链接本身是由root/root拥有的,但我相信应该是这样的,不是吗

我看到两个问题,我认为它们是相互关联的

首先,无论我如何启动脚本(使用init.d启动或使用“sudo service myapp start”手动启动),它似乎都是以root身份运行的(具体地说,应用程序试图用来访问文件的路径是/root/myapp/files,而不是/home/appservice/myapp/files)

其次,应用程序将崩溃。。。具体地说,我收到了系统日志消息“myapp.service start operation timed out.termining.”,随后是应用程序的有序关闭。哦,还有相关的。。。如果使用“sudo service myapp start”启动,则该命令永远不会返回。这是新的

第三,应用程序日志输出将转到/var/log/syslog,而不是/var/log/myapp.log,这似乎与Spring引导文档所说的相反

我正在为此进行部署的最终回归测试,(著名的遗言)我最近没有做任何更改…:)不,实际上,与发行版相关的最新更改是添加src/main/dist目录,之后它就开始工作了(在引导时以正确的用户身份启动)

我会发布启动脚本,但是当您使用springBoot{executable=true}任务时,它是springBoot提供的默认脚本

这是我的完整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

另一个可能与之相关的奇怪之处似乎指向某种用户/组/权限的混乱。。。当我将zip文件scp过来时,它最终位于user/group-appservice/appservice中。当我尝试将文件chgrp到组ip时,即使用户appservice是组pi和组appservice的成员,我也会得到“不允许操作”。。。如“info appservice”所确认的,如果您愿意并且能够切换到systemd来管理您的应用程序,请查看以下答案:是!看起来效果不错,伊格洛特。谢谢你。如果你能转寄这个作为答复,我会接受的。我仍然不知道这是否重要,只是想给你解决我的问题的荣誉。我发布了一个适合你情况的答案。