Systemctl for Tomcat in RedHat Enterprise Linux 7

If tomcat requires parameters that have been set in .bash_profile, below are steps to configure systemctl:

1) Create files with below content (check ownership and permission)

#!/bin/sh
#/appl/myservice/scripts/start.sh

SERVICE=’myservice’
SERVICE_USER=’tc_myservice’

START_TOMCAT=”/opt/tomcat.${SERVICE}/bin/startup.sh”

if [ X”$USER” = X”$SERVICE_USER” ]; then
${START_TOMCAT}
else
su – ${SERVICE_USER} -c ${START_TOMCAT}
fi
#!/bin/sh
#/appl/myservice/scripts/stop.sh

SERVICE=’myservice’
SERVICE_USER=’tc_myservice’

STOP_TOMCAT=”/opt/tomcat.${SERVICE}/bin/shutdown.sh”

if [ X”$USER” = X”$SERVICE_USER” ]; then
${STOP_TOMCAT}
else
su – ${SERVICE_USER} -c ${STOP_TOMCAT}
fi

 

2) Create file /etc/systemd/system/tomcat.myservice.service

[Unit]
Description=Tomcat process for myservice
After=multi-user.target

[Service]
Type=forking
User=tc_myservice
Group=tc_myservice
ExecStartPre=/bin/bash –login -c ‘env > /tmp/.myservice-environment-file’
ExecStart=/appl/myservice/scripts/start.sh
ExecStop=/appl/myservice/scripts/stop.sh
EnvironmentFile=-/tmp/.myservice-environment-file
Restart=no

[Install]
WantedBy=multi-user.target

 

3) Enable the service from systemctl
systemctl enable tomcat.myservice
systemctl daemon-reload

 

4) Test
systemctl start tomcat.myservice
systemctl status tomcat.myservice -l

systemctl stop tomcat.myservice
systemctl status tomcat.myservice -l

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s