0

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
Ali
  • 1,759
  • 2
  • 32
  • 69
  • Please provide your systemd configuration file of the service. – cmoetzing Jun 18 '19 at 08:40
  • Are you sure your application is running fine without spring boot – rohit prakash Jun 18 '19 at 08:42
  • Possible duplicate, see here : https://stackoverflow.com/questions/12102270/run-java-jar-file-on-a-server-as-background-process – Tom Jun 18 '19 at 08:42
  • @rohitprakash Yes, it looks fine. – Ali Jun 18 '19 at 08:45
  • Does the server stay running if you remove the `nohup` from the startup script? – CryptoFool Jun 18 '19 at 08:47
  • @Steve I haven't checked that from the script, but when I run it without nohup command from a terminal it works without any issues. – Ali Jun 18 '19 at 08:49
  • I don't think you want or need `nohup` when systemd is running your app. `nohup` is used specifically for launching background processes at a terminal prompt. I don't know why it would hurt anything, but as you mention it specifically as contributing to your problem, I think it would be good to know if that's actually true. It's possible that it and systemd don't play well together. – CryptoFool Jun 18 '19 at 09:01

1 Answers1

2

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.

cmoetzing
  • 742
  • 3
  • 16