首页 文章

以root身份而不是用户启动init.d的Spring启动可执行jar

提问于
浏览
2

救命!

我正在使用SpringBoot 1.3.6和Gradle的内置启动脚本 . 哦,还有解压缩任务的distZip任务 .

不久之前,这一切都运作得很好......然后我做了 - 我不知道是什么 - 搞砸了 .

我已经在我的覆盆子Pi上安装了包(基本上解压缩了Zip),并检查了所有权和权限 . 一切都归我要运行应用程序的用户所拥有(用户“appservice”组“pi”),并确认文件的权限是 - 如果有的话,太宽松了(755 for myapp / bin / myapp脚本和几乎其他一切) .

我在/etc/init.d中放了一个指向~appservice / myapp / bin / myapp的符号链接,我运行了update-rc.d myapp默认值来进入系统 . 注意符号链接本身由root / root拥有,但我相信它应该是,不是吗?

我看到两个问题,我认为这些问题是相互关联的 .

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

其次,应用程序将崩溃...具体来说,我得到系统日志消息“myapp.service start operation out out out . 终止 . ”然后看起来像是有序关闭应用程序 . 哦,相关...如果我用“sudo service myapp start”启动,命令永远不会返回 . 这是新的......

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

我正在为此部署最终的回归测试,并且(着名的最后一句话)我最近没有改变任何东西...... :)不,真的,与分发相关的最新变化是添加了src / main / dist目录,之后它正在工作(作为正确的用户在启动时启动) .

我发布了启动脚本但是AFAIK它是Spring Boot使用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'
}

1 回答

  • 1

    我建议使用 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
    

相关问题