How to Always start tomcat with a specified JVM property? - java

I am on Amazon EC2 instance. I started my tomcat using sudo service tomcat start. When I did ps -ef | grep tomcat, I got the following.
/usr/lib/jvm/jre/bin/java -classpath
/usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-
juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -
Dcatalina.home=/usr/share/tomcat -Djava.endorsed.dirs= -
Djava.io.tmpdir=/var/cache/tomcat/temp -
Djava.util.logging.config.file=/usr/share/tomcat/conf/logging.properties -
Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
org.apache.catalina.startup.Bootstrap start
I want to add another JVM system property Dlog=/var/tomcat/access/, and each time tomcat starts by sudo service tomcat start, I want it to start with the new property. How can I start it ?

Related

How to install Tomcat container for Docker in Fedora 28?

I am new in Docker, I would like to know how to install Tomcat Container from the command line, also, what are the pre-requisites, do I need to download Java on Fedora 28 first? Or Tomcat already contains a JVM ?
The tomcat/8.5/jre8/Dockerfile image definition starts with
FROM openjdk:8-jre
So it already includes a JDK.
All you need to do is run the default Tomcat server (CMD ["catalina.sh", "run"]):
$ docker run -it --rm tomcat:8.0
You can test it by visiting http://container-ip:8080
See more at hub.docker.com/_/tomcat/.
All you need is to install docker first.
The OP adds:
To be able to connect I had to run below command to get the container ID with
docker ps
docker inspect <containerid> | grep "IPAddress"

Can't start tomcat as service in redhat

I'm new to Linux but having spent a whole day I Installed Java and Tomcat. My goal is to host an App with this Linux box. I know it all works fine from my windows based machine, but it is my laptop so I'm planning to use the Linux Box as my dedicated server.
I am following this tutorial . From this tutorial I have executed the following command :
cd /etc/init.d
vi tomcat
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/java/jdk1.7.0_05
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/apache-tomcat-7.0.29
case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
esac
exit 0
chmod 755 tomcat
chkconfig --add tomcat
chkconfig --level 234 tomcat on
chkconfig --list tomcat
service tomcat start
After this command , tomcat is started at port 8082 . But when I restart pc , the tomcat is not started with boot of PC .
How can I do this ?
Since you use Red Hat you can use systemd for services.
Create a file /etc/systemd/system/tomcat.service:
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/java/jdk1.7.0_05
Environment=CATALINA_PID=/usr/share/apache-tomcat-7.0.29/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/share/apache-tomcat-7.0.29
Environment=CATALINA_BASE=/usr/share/apache-tomcat-7.0.29
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Duser.timezone=UTC -Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/usr/share/apache-tomcat-7.0.29/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
I specified the script to start after syslog and network are enabled.
As we can see systemd handles the tomcat as a daemon and kills the PID.
With User and Group we specify the user and the group that the process should be run as.
Systemd will handle the upstart process and kill it using the PID.
To enable it to run then issue:
systemctl enable tomcat
systemctl start tomcat
Try to use this command instead:
sudo systemctl enable tomcat

setenv.sh causing Tomcat7 to "fail" start

The problem is not as easy as it seems in the title.
I'm using a default Tomcat 7 package on Unbutu 14.04 LTS. When I have no "setenv.sh" in /usr/share/tomcat7/bin, it starts saying "OK" when I do :
$ sudo service tomcat7 start
* Starting Tomcat servlet engine tomcat7 [OK]
When I use the setenv.sh described below, it ALSO STARTS with no error in /var/lib/logs/catalina.out but the service is detected as "failed" when /etc/init.d/tomcat7 calls "start-stop-daemon --test" and concludes it's not running :
$ sudo service tomcat7 start
* Starting Tomcat servlet engine tomcat7 [fail]
What can I do about this ?
/usr/share/tomcat7/bin/setenv.sh :
#! /bin/sh
export JAVA_HOME="/home/linc/install/jdk1.7.0_75"
(...)
# Check for application specific parameters at startup
if [ -r "$CATALINA_BASE/bin/appenv.sh" ]; then
. "$CATALINA_BASE/bin/appenv.sh"
fi
There is an other problem, maybe related : when I check the process running after the start detected as "failed" (ps -ef | grep java), I can see all -D options added by setenv.sh, but I can't see the -D option added by "appenv.sh" (though setenv.sh and appenv.sh have exactly the same 755 rights).
Note : if I launch sudo /usr/share/tomcat7/bin/startup.sh, the setenv.sh doesn't cause any problem and appenv.sh is used.
EDIT : I may have found the cause but not the explanation : when I remove the declaration of JAVA_HOME, it uses the default jvm and the service start is detected as "OK", but when I specify the home of the default jvm , it fails again !
export JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre"
or :
export JAVA_HOME="/usr/lib/jvm/java-8-oracle"
What is happening here ?
Here are the explanations.
The command testing wether the service is up or not excpects not only a PID but also a precise java binary :
start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
--user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
>/dev/null;
This is run 2 times, one before "catalina.sh" (and setenv.sh) is run, one after.
The "standard" tomcat conf on ubuntu works like this : /etc/init.d/tomcat7 can be overrided by /etc/default/tomcat7 who can be overrided by catalina.sh (+ setenv.sh + appenv.sh).
So there are run with 2 different JAVA_HOME, first one with the one in /etc/default/tomcat7 or some auto-detected one, and second one with the one set in setenv.sh. This makes the start-stop-daemon test fail.
The solution would be to set JAVA_HOME twice, one in /etc/default/tomcat7 for the service launch, and one in setenv.sh in case some direct launch (via startup.sh in shell) needs to be done for test purpose, with some comments warning about the duplication.
About appenv.sh, the reason is CATALINA_BASE == CATLINA_HOME only when you start Tomcat from command line (startup.sh). When running Tomcat as a service CATALINA_BASE = /var/lib/tomcat7 while $CATALINA_HOME = /usr/share/tomcat7.
So putting setenv.sh (and appenv.sh) in /var/lib/tomcat7/bin/ instead of /usr/share/tomcat7/bin solve the problem.

Java Heroku application run locally with foreman

I'm following the "Getting Started with Java on Heroku" guide at
https://devcenter.heroku.com/articles/getting-started-with-java
I follow the steps till I deploy and execute succesfully the application downloaded from GitHub.
When I try to execute it locally on Windows XP with the command
foreman start web
I get the error:
web.1 | started with pid 3388
web.1 | Error: Could not find or load main class Main
web.1 | exited with code 1
system | sending SIGKILL to all processes
My Procfile is:
web: java %JAVA_OPTS% -cp target\classes:target\dependency\* Main
And
>echo %JAVA_OPTS%
-Xms256m -Xmx512m
Can anyone suggest me how to solve?
Quotes and semicolon
web: java %JAVA_OPTS% -cp target\classes;"target\dependency\*" Main
I ran into this issue while running through the https://devcenter.heroku.com/articles/getting-started-with-java tutorial.
After tinkering with some of these answers I discovered that step six at https://devcenter.heroku.com/articles/getting-started-with-java#define-a-procfile tells the answer.
When you see instructions to run your app with foreman, append an extra -f Procfile.windows flag to ensure your Windows-specific Procfile is picked up. For example: foreman start web -f Procfile.windows
Once I switched to the foreman start web -f Procfile.windows command, everything worked smoothly.
Same problem with java-getting-stared app downloaded from heroku server. Changing to ";" works on Windows. Still need ":" on heroku linux server.

Tomcat shutdown: The process cannot access the file

people.
Can somebody please help with this...
After Tomcat started by running startup.bat - need to stop it but get:
c:\Opt\tomcat_8080\bin>shutdown.bat
Using CATALINA_BASE: c:\Opt\tomcat_8080
Using CATALINA_HOME: c:\Opt\tomcat_8080
Using CATALINA_TMPDIR: c:\Opt\tomcat_8080\temp
Using JRE_HOME: C:\Opt\jdk1.6.0_32x64\jre
Using CLASSPATH: c:\Opt\tomcat_8080\bin\bootstrap.jar
The process cannot access the file because it is being used by another process.
I can't use taskkill - because there is few java.exe processes - so I can't get PID of just Tomcat... Just kill it manually from Process Explorer - but need to do it automatically, from script which will be called from TeamCity server.
OS - Windows 7, Tomcat 5.5.36.
Solved by:
1) Added line to /bin/startup.bat:
set title="Tomcat"
So now I have description of Java process in tasklist (needed because there is few Java process and need to kill only Tomcat).
2) First command - select process with Title Tomcat and write it in to file:
>tasklist /v /FI "IMAGENAME eq java.exe" | findstr /i "Tomcat" > tomcatpid.txt
3) Second command - select TOKEN 2 (which is PID of previously selected process) and run taskkill:
>for /F "TOKENS=2" %a in ('type tomcatpid.txt') do (taskkill /PID %a)
>(taskkill /PID 3360 )
SUCCESS: Sent termination signal to the process with PID 3360.
Hope - this will help somebody :-)
I sure there more simple solution - but writing scripts for Windows are mnot my favorite occupation...

Categories