Over a million developers have joined DZone.

Systemd and Upstart Services

Adding daemons to a Debian or Red Hat system is commonplace; this procedure shows you how to install and run Apache Tomcat with the aid of Systemd.

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Most Linux servers that I use are either Debian based or Red Hat based.

A common task is adding daemon services.

Suppose that we want to start a tomcat application on startup

First we install tomcat:

mkdir /opt/tomcat
groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
wget http://apache.cc.uoc.gr/tomcat/tomcat-8/v8.0.33/bin/apache-tomcat-8.0.33.tar.gz
tar xvf apache-tomcat-8.0.33.tar.gz
mv apache-tomcat-8.0.33/* /opt/tomcat
rm -r apache-tomcat-8.0.33 apache-tomcat-8.0.33.tar.gz 
cd /opt/tomcat
chgrp -R tomcat conf
chmod g+rwx conf
chmod g+r conf/*
chown -R tomcat work/ temp/ logs/

In case of Systemd we should add a tomcat.service file in /etc/systemd/system.

/etc/systemd/system/tomcat.service:

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/java/default
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Duser.timezone=UTC -Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target


I specified the script to start after syslog and network are enabled. As we can see systemd handles tomcat as a daemon and kills the pid. With User and Group we specify the user and the group that the process should be run as. Systemd will handle the upstart process and kill it using the PID.

Enter these command to enable and run:

systemctl enable tomcat
systemctl start tomcat


In case of upstart we should create a tomcat.conf file in /etc/init/:

description "Tomcat instance"
author "Emmanouil Gkatziouras"

respawn
respawn limit 2 5

start on runlevel [2345]
stop on runlevel [!2345]

setuid tomcat
setgid tomcat

env CATALINA_HOME=/opt/tomcat

script
 $CATALINA_HOME/bin/catalina.sh run
end script

post-stop script
 rm -rf $CATALINA_HOME/temp/*
end script

It will start on run levels 2,3,4 or 5.

The group and the user id to be executed will be tomcat. After tomcat is stopped the post script block will remove the temp files. Instead of starting the process in the background as a daemon, upstart will handle the process on the foreground.

To start:

sudo initctl start tomcat

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
spring ,aws ,sqs ,jms

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}