-
-
Save zengxs/a697d786b244d7b857d3d006213306a1 to your computer and use it in GitHub Desktop.
# 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=CATALINE_BASE=/opt/tomcat | |
Environment='CATALINE_OPTS=-Xms128M -Xmx765M -server -XX:+UseParallelGC' | |
Environment='JAVA_OPTS=-Djava.awt.haedless=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 |
Just add WorkingDirectory=/usr/share/apache-tomcat to your service and you'll be fine (or use an absolute path on velocity.log)
@beuss Thanks man, it worked
I had to use the following, because startup.sh returns immediatly and systemd.calls shutdown.sh.
My script uses catalina.sh run, which will run in the current window
[Unit]
Description=Tomcat
After=network.target
[Service]
User=tomcat
WorkingDirectory=/opt/tomcat
Environment=JRE_HOME=/usr/java/latest
Environment=JAVA_HOME=/usr/java/latest
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINE_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/catalina.sh run
ExecStop=/opt/tomcat/bin/shutdown.sh
SyslogIdentifier=tomcat-%i
[Install]
WantedBy=multi-user.target
Thanks a lot @oers!
I had to use ExecStart=/opt/tomcat/bin/catalina.sh start
.
Under upstart
I had always used /opt/tomcat/bin/catalina.sh run
. When I switched over to systemd
I found that using this command made it so that systemd didn't properly start my service. The command systemctl start tomcat
would not return back to the shell. Tomcat would start up fine but the service wasn't happy and would eventually timeout taking tomcat down with it. Using /opt/tomcat/bin/startup.sh
worked but my set environment variables were ignored. I found that using /opt/tomcat/bin/catalina.sh start
solved both of those problems.
I had to use the following, because startup.sh returns immediately and systemd.calls shutdown.sh.
@oers Do you know why this happens?
I faced the same problem - and this workaround works, I am just trying to figure out why.
You might find this interesting
https://jdebp.eu/FGA/systemd-house-of-horror/tomcat.html
Only way to make tomcat 9.0.14 work with systemd units on Centos 7.6 was with oers example.
Here is what I use (with custom OPTS):
[Unit]
Description=Tomcat
After=network.target
[Service]
User=tomcat
WorkingDirectory=/opt/tomcat
Environment=JRE_HOME=/usr/lib/jvm/jre
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINE_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Dfile.encoding=ISO-8859-1'
Environment='JAVA_OPTS=-server -Xms512M -Xmx1G -Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom -XX:+UseConcMarkSweepGC -Djava.net.preferIPv4Stack=true -Dsun.net.inetaddr.ttl=0'
ExecStart=/opt/tomcat/bin/catalina.sh run
ExecStop=/opt/tomcat/bin/shutdown.sh
SyslogIdentifier=tomcat-%i
[Install]
WantedBy=multi-user.target
Type=forking
-> catalina.sh start
or startup.sh
Type=simple
(default?) -> catalina.sh run
@mr-rycho In the Type=simple model, the process spawned by systemd is the dæmon
catalinE ?! CATALINUS is much better )
Hi,
I tried the script. I need tomcat to be run as tomcat user and start automatically at the system boot. Everything's work fine except I am getting an exception in the startup log.
this exception is not there when I start tomcat from CATALINA_HOME/bin directory.
I checked the file permission of velocity.log and it looks fine.
Below shows my script:
Please advise!