Code Mozzer

and if you have five seconds to spare, then I'll tell you the story of my life

daemontools Tutorial - run service as daemon. Java Spring Boot and Play Scala Production deployment

• daemontools

daemontools setup

Here will be explained how to setup Daemon Tools for Ubuntu environment, but is similar for other environments too.

What is Daemon Tools:

daemontools is a collection of tools for managing UNIX services. supervise monitors a service, it starts the service and restarts the service if it dies. Setting up a new service is easy, all supervise needs is a directory with a run script that runs the service.

1. install daemontools:

sudo apt-get install daemontools

2. After installation is done we need to make daemontools run on startup and to do that we need to create svscan.conf under /etc/init dir. Content of svscan.conf file is described at Start Daemon Tools .

# svscan - daemontools
#
# This service starts daemontools
start on runlevel [12345]
stop on runlevel [^12345]
respawn
exec /usr/bin/svscanboot

We need now to start svscan service via command

sudo service svscan start

Also make svscan to run on system boot.

svscan.conf explained:

daemontools service structure

In order to setup daemontools service we need to follow rules. To create service with name service-example, service-example directory needs to be created under /etc/service directory, as that is path which daemontools is scanning for services.

service-example directory needs to have following files and directories:

envdir runs another program with environment modified according to files in a specified directory.

Control daemontools services

Once we have daemontools service setup, it can be easily started, stopped and paused via svc command. svc controls services monitored by supervise.

All commands are listed in svc documentation.
With these commands we can easily stop service in case of new deployment and easily restart it later. daemontools will start all services on system boot.

Java Spring Boot daemontools service configuration example

When we build our spring boot application, we get service-example.jar. If we copy builded jar to /opt/jar folder then our service can be started by following command:

java -jar /opt/jar/service-example.jar --spring.profiles.active=production

spring-boot service-example application daemontools setup:

Play Scala daemontools service configuration example

Play framework scala application is builded via command:

activator dist service-example-app

zip file service-example-app/target/universal/service-example-app-1.0-SNAPSHOT.zip is generated. In order to start application we need to extract generated .zip file. If we copy extract zip file to /opt/app folder then our service can be started by following command:

sh "/opt/app/service-example-app/service-example/bin/service-example" -Dconfig.file=/etc/app.conf -Dlogger.file=/etc/app_logback.xml

With play scala application daemontools setup stop script is added as RUNNING_PID is not automatically removed on service stop, so this script is deleting RUNNING_PID executing command:

sudo rm "/opt/app/service-example-app/service-example/RUNNING_PID"

If RUNNING_PID file is not removed then play app cannot be started again as pid already exists.

play framework scala service-example application daemontools setup:

comments powered by Disqus