How to use daemonize to always keep the tomcat process running

daemonjava

I was researching how to keep a background process running and I came across daemonize. This answer says that I can use daemonize to ensure a process is always running: "Ensure a process is always running". I went thru the man page: http://software.clapper.org/daemonize/daemonize.html .

It only closes stdin, stdout, stderr, changing work directory to root, etc. It's doing all the things that a good background process should do.

The only thing I found related to restarting was -p pidfile for outputting the process id and -l lockfile for making sure only one process is started.

Best Answer

If you don't want to use the tomcat package of your distribution, you can use systemd and define your own unit file such as:

# Systemd unit file for tomcat
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre
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=-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
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Read the tutorial How To Install Apache Tomcat 8 on CentOS 7 for full details.

Related Question