简体   繁体   中英

Run a java application in background on Debian with logback and nohup

I have some apps that are packaged as executable jar files. Now I added logback logging with file appender to the app. The problem is I run those apps on server with this command:

nohup java -jar app.jar &

nohup.out captures all outputs and ruins the whole purpose of rotating log files and separate error and info log. I'd rather not to run my app as a service (There are lots of it) so wondering if there is a way to execute the jar in the background infinitely and have the logback logging to the files defined in logback.xml This is my logback.xml file if it helps:

<configuration debug="true">

    <appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>

        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>/home/user/logs/kafka/orders/kafka-consumer.info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <totalSizeCap>3GB</totalSizeCap>
            <maxHistory>200</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>%d %p %c{1.} %m%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE-ROLLING-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>

        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>/home/user/logs/kafka/orders/kafka-consumer.error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <totalSizeCap>3GB</totalSizeCap>
            <maxHistory>200</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>%d %p %c{1.} %m%n%ex{100}</pattern>
        </encoder>
    </appender>

    <logger name="kafkaLogger" level="info" additivity="false">
        <appender-ref ref="FILE-ROLLING"/>
    </logger>
    <logger name="kafkaLoggerError" level="error" additivity="false">
        <appender-ref ref="FILE-ROLLING-ERROR"/>
    </logger>

    <root level="info">
        <appender-ref ref="FILE-ROLLING"/>
        <appender-ref ref="FILE-ROLLING-ERROR"/>
    </root>

</configuration>

您需要将配置文件传递到登录系统。

 nohup java -jar app.jar -DLogback.configurationFile=/path/to/Logback.xml &

我认为执行此操作的方法之一是这样的:

nohup setsid java -jar app.jar -Dparams=xyz </dev/null >/dev/null 2>&1 &

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM