You are not logged in.

#1 2018-12-06 08:47:57

nasser
Member
Registered: 2016-01-23
Posts: 56

[SOLVED]Can't run Tomcat as a service

I have downloaded Tomcat binary distribution and it works fine if I run it from startup.sh script, however when I attempt to to create a service file it starts successfully without no errors but Tomcat doesn't run.

Here is the service file which is located in

/etc/systemd/user/tomcat.service
[Unit]
Description=Apche Tomcat Web Application Container

[Service]
ExecStart=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/bin/startup.sh

[Install]
WantedBy=multi-user.target

output of

sudo systemctl status tomcat.service
● tomcat.service - Apche Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/user/tomcat.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Thu 2018-12-06 10:43:29 EET; 5min ago
  Process: 3248 ExecStart=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/bin/startup.sh (code=exited, >
 Main PID: 3248 (code=exited, status=0/SUCCESS)

Dec 06 10:43:29 NasserLaptop systemd[1]: Started Apche Tomcat Web Application Container.
Dec 06 10:43:29 NasserLaptop startup.sh[3248]: Tomcat started.

But it doesn't start at all
*Note: I have tried scripts from the internet but it failed too, so according to forum rules I have posted my own solution.

Last edited by nasser (2018-12-07 12:41:02)

Offline

#2 2018-12-06 09:25:03

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,420

Re: [SOLVED]Can't run Tomcat as a service

The tomcat startup script contains forking calls by default, your script will end after the actual process has forked, but the actual process gets killed immediately, because you didn't tell systemd that you intend to start a forking service. You will at least want to have type forking and references to the PID file. You should look at the current package  PKGBUILD for some pointers on how it's supposed to look like.

FWIW if you intend to run this in your home directory and as your own user, you should also consider a proper user service instead of a system service (which would mean you want wantedby on default.target and not multi-user.target).

Last edited by V1del (2018-12-06 09:29:12)

Offline

#3 2018-12-06 10:29:06

nasser
Member
Registered: 2016-01-23
Posts: 56

Re: [SOLVED]Can't run Tomcat as a service

V1del wrote:

The tomcat startup script contains forking calls by default, your script will end after the actual process has forked, but the actual process gets killed immediately, because you didn't tell systemd that you intend to start a forking service. You will at least want to have type forking and references to the PID file. You should look at the current package  PKGBUILD for some pointers on how it's supposed to look like.

FWIW if you intend to run this in your home directory and as your own user, you should also consider a proper user service instead of a system service (which would mean you want wantedby on default.target and not multi-user.target).

I have made a separate EnvironmentFile:

CATALINA_HOME=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
CATALINA_BASE=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
CATALINA_TMPDIR=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/temp
JAVA_HOME=/usr/lib/jvm/java-8-openjdk
CLASS_PATH=${CLASSPATH}:${CATALINA_BASE}/bin/bootstrap.jar:${CATALINA_BASE}/bin/tomcat-juli.jar
JRE_HOME=${JAVA_HOME}
ERRFILE=SYSLOG
OUTFILE=SYSLOG
PIDFILE=/var/run/tomcat.pid
CATALINA_OPTS=

And edited the tomcat.service file

[Unit]
Description=Apche Tomcat Web Application Container

[Service]
Type=forking
EnvironmentFile=-/etc/default/tomcat
ExecStart=/usr/bin/env ${JAVA_HOME}/bin/java \
-Dcatalina.base=${CATALINA_BASE} \
-Dcatalina.home=${CATALINA_HOME} \
-Djava.io.tmpdir=${CATALINA_TMPDIR} \
-Djava.util.logging.config.file=${CATALINA_BASE}/conf/logging.properties \
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager \
-classpath ${CLASS_PATH} \
org.apache.catalina.startup.Bootstrap \
start

[Install]
WantedBy=default.target

And it gives me the following error

● tomcat.service - Apche Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/user/tomcat.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2018-12-06 12:29:23 EET; 51s ago
  Process: 5132 ExecStart=/usr/bin/env ${JAVA_HOME}/bin/java -Dcatalina.base=${CATALINA_BASE} -Dcatalina.home=${CATALINA_HOME} -Djava.io.tmpdir=${CATALINA_TMPDIR} -Djava.util.logging.config.file=${CATALINA_BASE}/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -classpath ${CLASS_PATH} org.apache.catalina.startup.Bootstrap start (code=exited, status=1/FAILURE)
 Main PID: 4876 (code=exited, status=1/FAILURE)

Dec 06 12:29:22 NasserLaptop systemd[1]: Starting Apche Tomcat Web Application Container...
Dec 06 12:29:23 NasserLaptop env[5132]: Error: Could not find or load main class org.apache.catalina.startup.Bootstrap
Dec 06 12:29:23 NasserLaptop systemd[1]: tomcat.service: Control process exited, code=exited status=1
Dec 06 12:29:23 NasserLaptop systemd[1]: tomcat.service: Failed with result 'exit-code'.
Dec 06 12:29:23 NasserLaptop systemd[1]: Failed to start Apche Tomcat Web Application Container.

*Note: I have tried to add -pidfile, -errfile and -outfile options but it gives me error

Unrecognized option 

Offline

#4 2018-12-06 15:16:59

twelveeighty
Member
From: Alberta, Canada
Registered: 2011-09-04
Posts: 1,096

Re: [SOLVED]Can't run Tomcat as a service

I think your issue has something to do with variable expansion. For what it's worth, here's a working service file from one of my TomEE clusters running on Debian. I don't think there are any relevant differences between the distros in terms of systemd configuration. Also be sure that file ownership and permissions match to the user that sytemd is launching the service with.

[Unit]
Description=TomEE Java EE Server
After=network.target

[Service]
Type=forking
PIDFile=/opt/apache-tomee-plus/temp/tomee.pid
User=tomcat
Group=tomcat
Environment=CATALINA_PID=/opt/apache-tomee-plus/temp/tomee.pid
Environment=JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
Environment=JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
Environment=CATALINA_HOME=/opt/apache-tomee-plus
Environment=CATALINA_BASE=/opt/apache-tomee-plus
Environment='CATALINA_OPTS=-Xms1024M -Xmx1024M -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true'

ExecStart=/opt/apache-tomee-plus/bin/startup.sh

ExecStop=/opt/apache-tomee-plus/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Offline

#5 2018-12-06 20:18:36

nasser
Member
Registered: 2016-01-23
Posts: 56

Re: [SOLVED]Can't run Tomcat as a service

twelveeighty wrote:

I think your issue has something to do with variable expansion. For what it's worth, here's a working service file from one of my TomEE clusters running on Debian. I don't think there are any relevant differences between the distros in terms of systemd configuration. Also be sure that file ownership and permissions match to the user that sytemd is launching the service with.

[Unit]
Description=TomEE Java EE Server
After=network.target

[Service]
Type=forking
PIDFile=/opt/apache-tomee-plus/temp/tomee.pid
User=tomcat
Group=tomcat
Environment=CATALINA_PID=/opt/apache-tomee-plus/temp/tomee.pid
Environment=JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
Environment=JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
Environment=CATALINA_HOME=/opt/apache-tomee-plus
Environment=CATALINA_BASE=/opt/apache-tomee-plus
Environment='CATALINA_OPTS=-Xms1024M -Xmx1024M -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true'

ExecStart=/opt/apache-tomee-plus/bin/startup.sh

ExecStop=/opt/apache-tomee-plus/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

I have added the contents of the environment file into the main service file, and make

ExecStart=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/bin/startup.sh

but it gives me the following error

● tomcat.service - Apche Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/user/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-12-06 22:12:41 EET; 7min ago
 Main PID: 1749 (java)
    Tasks: 57 (limit: 4240)
   Memory: 680.1M
   CGroup: /system.slice/tomcat.service
           └─1749 /usr/lib/jvm/java-8-openjdk/bin/java -Djava.util.logging.config.file=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/bin/bootstrap.jar:/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/bin/tomcat-juli.jar -Dcatalina.base=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35 -Dcatalina.home=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35 -Djava.io.tmpdir=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/temp org.apache.catalina.startup.Bootstrap start

Dec 06 22:12:41 NasserLaptop systemd[1]: Starting Apche Tomcat Web Application Container...
Dec 06 22:12:41 NasserLaptop startup.sh[1740]: Tomcat started.
Dec 06 22:12:41 NasserLaptop systemd[1]: Started Apche Tomcat Web Application Container.
Dec 06 22:12:45 NasserLaptop systemd[1]: /etc/systemd/user/tomcat.service:7: Unknown lvalue 'Environmint' in section 'Service'

Even if I removed the all Environment keywords and reloading then starting the service it gives me the same error

Offline

#6 2018-12-06 22:18:32

twelveeighty
Member
From: Alberta, Canada
Registered: 2011-09-04
Posts: 1,096

Re: [SOLVED]Can't run Tomcat as a service

nasser wrote:
Unknown lvalue 'Environmint' in section 'Service'

C'mon man, "Environmint" should obviously be "Environment" - you have a typo in your .service file. If that's not it, post the service file.

Offline

#7 2018-12-07 05:24:00

nasser
Member
Registered: 2016-01-23
Posts: 56

Re: [SOLVED]Can't run Tomcat as a service

twelveeighty wrote:
nasser wrote:
Unknown lvalue 'Environmint' in section 'Service'

C'mon man, "Environmint" should obviously be "Environment" - you have a typo in your .service file. If that's not it, post the service file.

Now it gives me a new error:

● tomcat.service - Apche Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/user/tomcat.service; enabled; vendor preset: disabled)
   Active: failed (Result: timeout) since Fri 2018-12-07 07:24:48 EET; 57s ago
  Process: 3136 ExecStart=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 1606 (code=exited, status=0/SUCCESS)

Dec 07 07:23:17 NasserLaptop systemd[1]: Starting Apche Tomcat Web Application Container...
Dec 07 07:23:18 NasserLaptop startup.sh[3136]: Tomcat started.
Dec 07 07:23:18 NasserLaptop systemd[1]: tomcat.service: Failed to parse PID from file /home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/temp/tomcat.pid: Invalid argument
Dec 07 07:24:48 NasserLaptop systemd[1]: tomcat.service: Start operation timed out. Terminating.
Dec 07 07:24:48 NasserLaptop systemd[1]: tomcat.service: Failed with result 'timeout'.
Dec 07 07:24:48 NasserLaptop systemd[1]: Failed to start Apche Tomcat Web Application Container.

Service file:

[Unit]
Description=Apche Tomcat Web Application Container

[Service]
Type=forking
PIDFile=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/temp/tomcat.pid
Environment=CATALINA_HOME=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
Environment=CATALINA_BASE=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
Environment=CATALINA_TMPDIR=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/temp
Environment=JAVA_HOME=/usr/lib/jvm/java-8-openjdk
Environment=CLASS_PATH=${CLASSPATH}:${CATALINA_BASE}/bin/boINA_BASE}
Environment=JRE_HOME=${JAVA_HOME}
Environment=ERRFILE=SYSLOG
Environment=OUTFILE=SYSLOG
Environment=PIDFILE=/var/run/tomcat.pid
Environment=CATALINA_OPTS=

ExecStart=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/bin/startup.sh

[Install]
WantedBy=default.target

Offline

#8 2018-12-07 07:10:31

gerald82
Member
From: The known universe
Registered: 2011-03-25
Posts: 83

Re: [SOLVED]Can't run Tomcat as a service

You could try storing all the environment veriables in a file and then pass it to the service with 'EnvironmentFile' instead of 'Environment'.
Like so:

/tmp/tomcat-env

CATALINA_HOME=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
CATALINA_BASE=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
.
.
.

tomcat unit file:

[Service]
.
.
.
EnvironmentFile=-/tmp/tomcat-env
.
.
.

I had a similar issue when setting up a service that brings its own tomcat instance and solved it like that. Except that in my case the environment is generated each time the service is run (which is why I actually do this in /tmp instead of storing the file somewhere persistent)

Offline

#9 2018-12-07 07:31:45

gerald82
Member
From: The known universe
Registered: 2011-03-25
Posts: 83

Re: [SOLVED]Can't run Tomcat as a service

nasser wrote:

Now it gives me a new error:

Dec 07 07:23:18 NasserLaptop systemd[1]: tomcat.service: Failed to parse PID from file /home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/temp/tomcat.pid: Invalid argument

From the docs I take it systemd deletes the PID file when the service is shut down and doesn't do so itself. Did you get this error in the first attempt or is it consecutive? Please confirm the file exists at all.
Also how is the PID file created?

Offline

#10 2018-12-07 09:00:07

nasser
Member
Registered: 2016-01-23
Posts: 56

Re: [SOLVED]Can't run Tomcat as a service

gerald82 wrote:

You could try storing all the environment veriables in a file and then pass it to the service with 'EnvironmentFile' instead of 'Environment'.
Like so:

/tmp/tomcat-env

CATALINA_HOME=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
CATALINA_BASE=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
.
.
.

tomcat unit file:

[Service]
.
.
.
EnvironmentFile=-/tmp/tomcat-env
.
.
.

I had a similar issue when setting up a service that brings its own tomcat instance and solved it like that. Except that in my case the environment is generated each time the service is run (which is why I actually do this in /tmp instead of storing the file somewhere persistent)

That was my first attempt, but it looks like there was something error in variable expansion so I put it all together with absolute path.

Offline

#11 2018-12-07 09:01:39

nasser
Member
Registered: 2016-01-23
Posts: 56

Re: [SOLVED]Can't run Tomcat as a service

gerald82 wrote:
nasser wrote:

Now it gives me a new error:

Dec 07 07:23:18 NasserLaptop systemd[1]: tomcat.service: Failed to parse PID from file /home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/temp/tomcat.pid: Invalid argument

From the docs I take it systemd deletes the PID file when the service is shut down and doesn't do so itself. Did you get this error in the first attempt or is it consecutive? Please confirm the file exists at all.
Also how is the PID file created?

Actually it's my first to get this error, before that it said

no such directory or file

so I created it by using

echo "" >> tomcat.pid 

Offline

#12 2018-12-07 09:25:58

gerald82
Member
From: The known universe
Registered: 2011-03-25
Posts: 83

Re: [SOLVED]Can't run Tomcat as a service

nasser wrote:

Actually it's my first to get this error, before that it said

no such directory or file

so I created it by using

echo "" >> tomcat.pid 

That is definitely not the way to do it.
Not sure if Tomcat does that (or the startup script theirof more specifically), but it should. If it doesn't do that then I think you have no way to specify the PID (and PIDFile).
I'd say just ditch the PIDFile entry in the unit file. My knowledge on this is far from complete, but I think it's about getting the system logs correctly. But since Tomcat has it's own logging and probably wouldn't write much of interest to the system log anyway, I'd just ignore it.

Offline

#13 2018-12-07 12:38:23

nasser
Member
Registered: 2016-01-23
Posts: 56

Re: [SOLVED]Can't run Tomcat as a service

gerald82 wrote:
nasser wrote:

Actually it's my first to get this error, before that it said

no such directory or file

so I created it by using

echo "" >> tomcat.pid 

That is definitely not the way to do it.
Not sure if Tomcat does that (or the startup script theirof more specifically), but it should. If it doesn't do that then I think you have no way to specify the PID (and PIDFile).
I'd say just ditch the PIDFile entry in the unit file. My knowledge on this is far from complete, but I think it's about getting the system logs correctly. But since Tomcat has it's own logging and probably wouldn't write much of interest to the system log anyway, I'd just ignore it.

I have removed PIDFile line and now it works, u were right Tomcat uses its own logging and temp dirs

This script works fine for me:

[Unit]
Description=Apche Tomcat Web Application Container

[Service]
Type=forking
Environment=CATALINA_HOME=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
Environment=CATALINA_BASE=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35
Environment=CATALINA_TMPDIR=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/temp
Environment=JAVA_HOME=/usr/lib/jvm/java-8-openjdk
Environment=JRE_HOME=${JAVA_HOME}
Environment=ERRFILE=SYSLOG
Environment=OUTFILE=SYSLOG
Environment=CATALINA_OPTS=

ExecStart=/home/nasser/Programs/ApacheTomcat/apache-tomcat-8.5.35/bin/startup.sh

[Install]
WantedBy=default.target

Thx alot guys.

Last edited by nasser (2018-12-07 12:45:59)

Offline

Board footer

Powered by FluxBB