Systemd and Upstart Services

Most linux servers that I use are either Debian based or RedHat based. A common task is adding daemon services. Suppose that we want to start a tomcat application on startup First we shall install tomcat

mkdir /opt/tomcat
groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
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 on /etc/systemd/system. The file /etc/systemd/system/tomcat.service shall contain

Description=Apache Tomcat Web Application Container
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Duser.timezone=UTC -Djava.awt.headless=true'
ExecStop=/bin/kill -15 $MAINPID

I specified the script to start after syslog and network are enabled. As we can see systemd handles the 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. To enable and run you have to issue

systemctl enable tomcat
systemctl start tomcat

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

description     "Tomcat instance"
author          "Emmanouil Gkatziouras"
respawn limit 2 5
start on runlevel [2345]
stop on runlevel [!2345]
setuid tomcat
setgid tomcat
env CATALINA_HOME=/opt/tomcat
        $CATALINA_HOME/bin/ 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 would be tomcat. After tomcat is stopped the post script block will remove the temp files. Instead of starting the process inn the background as a daemon,, upstart will handle the process on the foreground. To start just issue

sudo initctl start tomcat
Reference: Systemd and Upstart Services from our SCG partner Emmanouil Gkatziouras at the Systemd and Upstart Services blog.

Emmanouil Gkatziouras

I am a versatile software engineer with experience in a wide variety of applications/services. I am enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button