Starting Tomcat at boot time with Ubuntu 18.04 using systemd

Hi,

I couldn’t get DHIS 2 (v 2.30 with OpenJDK) to reboot on Ubuntu 18.04 after power outages using the instructions here: https://docs.dhis2.org/2.30/en/implementer/html/install_starting_tomcat_boot_time.html.

Does 18.04 use systemd instead by default?

I have created a file (/lib/systemd/system/tomcat.service) with the following:

[Unit]
Description=Apache Tomcat Web Application Container

[Service]
User=dhis
Group=dhis
EnvironmentFile=-/etc/default/tomcat
ExecStart=/usr/bin/env ${JAVA_HOME}/bin/java \
$JAVA_OPTS \
-classpath ${CLASSPATH} \
-Dcatalina.base=${CATALINA_BASE} \
-Dcatalina.home=${CATALINA_HOME} \
-Djava.endorsed.dirs=${JAVA_ENDORSED_DIRS} \
-Djava.io.tmpdir=${CATALINA_TMPDIR} \
-Djava.util.logging.config.file=${CATALINA_BASE}/conf/logging.properties \
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager \
org.apache.catalina.startup.Bootstrap \
start
ExecStop=/usr/bin/env ${JAVA_HOME}/bin/java \
$JAVA_OPTS \
-classpath ${CLASSPATH} \
-Dcatalina.base=${CATALINA_BASE} \
-Dcatalina.home=${CATALINA_HOME} \
-Djava.endorsed.dirs=${JAVA_ENDORSED_DIRS} \
-Djava.io.tmpdir=${CATALINA_TMPDIR} \
-Djava.util.logging.config.file=${CATALINA_BASE}/conf/logging.properties \
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager \
org.apache.catalina.startup.Bootstrap \
stop

[Install]
WantedBy=multi-user.target

and a settings file (/etc/default/tomcat) with the following:

CATALINA_HOME=/usr/share/tomcat8
CATALINA_BASE=/home/dhis/tomcat-dhis
CATALINA_TMPDIR=/home/dhis/tomcat-dhis/temp
JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
JAVA_OPTS=-Xmx7500m -Xms4000m -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
JDK_JAVA_OPTIONS= --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
JAVA_ENDORSED_DIRS=
CLASSPATH=/usr/share/tomcat8/bin/bootstrap.jar:/usr/share/tomcat8/bin/tomcat-juli.jar
DHIS2_HOME=/home/dhis/config

[edit: followed by the command: sudo systemctl enable tomcat]

and it DHIS 2 now starts up automatically after reboot and seems to function correctly.

Are there any concerns about doing this?

Paul

1 Like

PS for anyone else trying to do something similar with a different setup, you can find out Java options etc by examining the Java process (use ps to find the process id first - in this case 863), e.g.:

paul@dhis2-primary:~$ ps aux|grep java
dhis       863 49.0 14.7 9767836 1774036 ?     Ssl  00:45   1:15 /usr/lib/jvm/java-8-openjdk-amd64/bin/java -Xmx7500m -Xms4000m -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -classpath /usr/share/tomcat8/bin/bootstrap.jar:/usr/share/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/home/dhis/tomcat-dhis -Dcatalina.home=/usr/share/tomcat8 -Djava.endorsed.dirs= -Djava.io.tmpdir=/home/dhis/tomcat-dhis/temp -Djava.util.logging.config.file=/home/dhis/tomcat-dhis/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager org.apache.catalina.startup.Bootstrap start
paul      4037  0.0  0.0  13136  1032 pts/0    S+   00:47   0:00 grep --color=auto java
paul@dhis2-primary:~$ sudo cat /proc/863/environ
[sudo] password for paul:
LANG=en_US.UTF-8PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOME=/home/dhisLOGNAME=dhisUSER=dhisSHELL=/bin/falseINVOCATION_ID=640ed00bfbd74a5683f90c7b452ad90cJOURNAL_STREAM=9:21790CATALINA_HOME=/usr/share/tomcat8CATALINA_BASE=/home/dhis/tomcat-dhisCATALINA_TMPDIR=/home/dhis/tomcat-dhis/tempJAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64JAVA_OPTS=-Xmx7500m -Xms4000m -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027JDK_JAVA_OPTIONS=--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMEDJAVA_ENDORSED_DIRS=CLASSPATH=/usr/share/tomcat8/bin/bootstrap.jar:/usr/share/tomcat8/bin/tomcat-juli.jarDHIS2_HOME=/home/dhis/config
1 Like

Interestingly, having done the above I found my other issue came back: https://community.dhis2.org/t/png-pdf-download-with-dhis-2-and-openjdk/36895

I added -Djava.awt.headless=true to JAVA_OPTS and commented out the assistive_technologies line in /etc/java-8-openjdk/accessibility.properties (all following much Googling) and this seems to have solved it, at least for the moment…

1 Like