Friday, October 29, 2021

[SOLVED] Spring boot application shutdown automatically when I use nohup

Issue

I have implemented a Spring boot application. This application works smoothly when I run it via console or in an IDE. However, when I register it as a systemd service via the nohup command it is shut down a few seconds after it is started automatically with showing the following lines in the application log:

2019-06-18 17:36:18,176 INFO org.springframework.scheduling.concurrent.ExecutorConfigurationSupport [Thread-4] Shutting down ExecutorService 'applicationTaskExecutor'
2019-06-18 17:36:18,184 INFO org.springframework.scheduling.concurrent.ExecutorConfigurationSupport [Thread-4] Shutting down ExecutorService 'threadPoolTaskScheduler'
2019-06-18 17:36:18,186 INFO org.springframework.orm.jpa.AbstractEntityManagerFactoryBean [Thread-4] Closing JPA EntityManagerFactory for persistence unit 'default'
2019-06-18 17:36:18,194 INFO com.zaxxer.hikari.HikariDataSource [Thread-4] HikariPool-1 - Shutdown initiated...
2019-06-18 17:36:18,244 INFO com.zaxxer.hikari.HikariDataSource [Thread-4] HikariPool-1 - Shutdown completed.

The systemd file (please ignore variables):

[Unit]
Description = Healthcheck notification Service
After network.target = {{healthcheck_notification_service_name}}.service
StartLimitBurst=5
StartLimitInterval=200
[Service]
Type=simple
User={{healthcheck_service_owner}}
Group={{healthcheck_service_owner}}
Restart=on-failure
RestartSec=30
SuccessExitStatus=143 
ExecStart = {{healthcheck_notification_installation_dir}}/{{healthcheck_notification_service_name}}/bin/service.sh start {{healthcheck_notification_service_name}}  {{healthcheck_notification_installation_dir}}/{{healthcheck_notification_service_name}}/lib/{{healthcheck_notification_installer_package_name}}.jar {{healthcheck_notification_installation_dir}}/{{healthcheck_notification_service_name}}/conf {{healthcheck_notification_installation_dir}}/{{healthcheck_notification_service_name}}/logs
ExecStop = {{healthcheck_notification_installation_dir}}/{{healthcheck_notification_service_name}}/bin/service.sh stop {{healthcheck_notification_service_name}}
ExecReload =  {{healthcheck_notification_installation_dir}}/{{healthcheck_notification_service_name}}/bin/service.sh restart {{healthcheck_notification_service_name}}  {{healthcheck_notification_installation_dir}}/{{healthcheck_notification_service_name}}/lib/{{healthcheck_notification_installer_package_name}}.jar {{healthcheck_notification_installation_dir}}/{{healthcheck_notification_service_name}}/conf {{healthcheck_notification_installation_dir}}/{{healthcheck_notification_service_name}}/logs

SyslogIdentifier={{healthcheck_notification_service_name}}
[Install]
WantedBy=multi-user.target

Startup script:

#!/bin/sh

SERVICE_NAME=$2
PATH_TO_JAR=$3
PATH_TO_CONF=$4
PATH_TO_LOG=$5
APP_CONF_NAME=$6
PID_PATH_NAME=/tmp/$SERVICE_NAME-pid
case $1 in
start)
       echo "Starting $SERVICE_NAME ..."
  if [ ! -f $PID_PATH_NAME ]; then
       nohup java -Dloader.path=$PATH_TO_CONF -Dlogging.path=$PATH_TO_LOG -jar $PATH_TO_JAR --spring.config.location=$PATH_TO_CONF/$APP_CONF_NAME --logging.config=$PATH_TO_CONF/logback-spring.xml 2>> $PATH_TO_LOG/service.err >> $PATH_TO_LOG/service.out &
       echo $! > $PID_PATH_NAME
       echo "$SERVICE_NAME started ..."
  else
       echo "$SERVICE_NAME is already running ..."
  fi
;;
stop)
  if [ -f $PID_PATH_NAME ]; then
         PID=$(cat $PID_PATH_NAME);
         echo "$SERVICE_NAME stoping ..."
         kill $PID;
         echo "$SERVICE_NAME stopped ..."
         rm $PID_PATH_NAME
  else
         echo "$SERVICE_NAME is not running ..."
  fi
;;
restart)
  if [ -f $PID_PATH_NAME ]; then
      PID=$(cat $PID_PATH_NAME);
      echo "$SERVICE_NAME stopping ...";
      kill $PID;
      echo "$SERVICE_NAME stopped ...";
      rm $PID_PATH_NAME
      echo "$SERVICE_NAME starting ..."
      nohup java -Dloader.path=$PATH_TO_CONF -Dlogging.path=$PATH_TO_LOG -jar $PATH_TO_JAR --spring.config.location=$PATH_TO_CONF/$APP_CONF_NAME --logging.config=$PATH_TO_CONF/logback-spring.xml 2>> $PATH_TO_LOG/service.err >> $PATH_TO_LOG/service.out &
      echo $! > $PID_PATH_NAME
      echo "$SERVICE_NAME started ..."
  else
      echo "$SERVICE_NAME is not running ..."
     fi     ;;
 esac

Solution

Systemd defines Type=simple as

A long-running process that does not background its self and stays attached to the shell

In your startup script you send your command into the background with nohup and & at the end of the command. Your type should probably be forking.

Here is also a good explanation.



Answered By - cmoetzing