Tomcat shutdown: The process cannot access the file - java

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...

Related

Empty Tomcat thread dumps using jstack

Trying to collect thread dump of Apache Tomcat8.5 in windows server I ended using jstack with psexec as follows(as using jstack directly wasn't possible, so I'm using pexec to execute jstack using syetm process):
PsExec.exe -s "C:\Program Files\Java\jdk1.7.0_40\bin\jstack.exe" -l 5340 > dumps.txt
with 5340 is the PID of the running Tomcat8 process.
The execution started without any errors and it shows this output:
PsExec.exe -s "C:\Program Files\Java\jdk1.7.0_40\bin\jstack.exe" -l 5340 > dumps.txt
PsExec v2.2 - Execute processes remotely Copyright (C) 2001-2016 Mark Russinovich Sysinternals - www.sysinternals.com
Starting C:\Program Files\Java\jdk1.7.0_40\bin\jstack.exe on VMNAME...
When I check th dumps.txt I find it empty, even I'm running The command line as Admin.
Thnks for your help!
As a conclusion to the discussion. You need to use the same VM version for executing a jstack command as the VM version you are going to inspect. In case of any errors, you could try a -F param to jstack.
Also, you don't need PsExec tools on newer VMs anymore.

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.

Glassfish is running in background

Glassfish Server is running in background and I am not able to stop, start or restart the process. I know, restarting the system will do the job. Is there any other process to stop the process?
Here is the details:
When I try to stop, it says it is domain1 is not running:
C:\Server\glassfish4\glassfish\bin>asadmin stop-domain
CLI306: Warning - The server located at C:\Server\glassfish4\glassfish\domains\domain1 is not running.
Command stop-domain executed successfully.
However I am able to open admin console in web browser for http://localhost:4848/common/index.jsf
When I am trying to start or restart it throws error:
C:\Server\glassfish4\glassfish\bin>asadmin start-domain There is a
process already using the admin port 4848 -- it probably is another
instance of a GlassFish server. Command start-domain failed.
C:\Server\glassfish4\glassfish\bin>asadmin restart-domain Server is
not running, will attempt to start it... There is a process already
using the admin port 4848 -- it probably is another instance of a
GlassFish server. Command restart-domain failed.
I tried to find PID with netstat -a -n -o command for the port 4848. I got two entries but not localhost id:
TCP 0.0.0.0:4848 0.0.0.0:0 LISTENING 9116
TCP [::]:4848 [::]:0 LISTENING 9116
Try this
taskkill /F /PID 9116
Also find the PIDs of processes which hold port 8080 and kill them. (If above solution don't work)
netstat -aon | find "LISTENING" | find ":8080"
taskkill /F /PID process_id_here
If are you using Mac OS you should open terminal and will write following:
jps
(jps - is command that help you to see PID of this process of GlassFish)
in my case I have following information:
MBP-Dmytro:~ melnychukdv$ jps
4004 ASMain
4500 Jps
After that we just kill this PID (in my case it is 4004):
sudo kill -4004 {PID}
or
sudo kill 4004 {PID}
That's all.

Run a Java Application as a Service on Linux

I have written a Java server application that runs on a standard virtual hosted Linux solution. The application runs all the time listening for socket connections and creating new handlers for them. It is a server side implementation to a client-server application.
The way I start it is by including it in the start up rc.local script of the server. However once started I do not know how to access it to stop it and if I want to install an update, so I have to restart the server in order to restart the application.
On a windows PC, for this type of application I might create a windows service and then I can stop and start it as I want. Is there anything like that on a Linux box so that if I start this application I can stop it and restart it without doing a complete restart of the server.
My application is called WebServer.exe. It is started on server startup by including it in my rc.local as such:
java -jar /var/www/vhosts/myweb.com/phpserv/WebServer.jar &
I am a bit of a noob at Linux so any example would be appreciated with any posts. However I do have SSH, and full FTP access to the box to install any updates as well as access to a Plesk panel.
I wrote another simple wrapper here:
#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
You can follow a full tutorial for init.d here and for systemd (ubuntu 16+) here
If you need the output log replace the 2
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
lines for
nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&
A simple solution is to create a script start.sh that runs Java through nohup and then stores the PID to a file:
nohup java -jar myapplication.jar > log.txt 2> errors.txt < /dev/null &
PID=$!
echo $PID > pid.txt
Then your stop script stop.sh would read the PID from the file and kill the application:
PID=$(cat pid.txt)
kill $PID
Of course I've left out some details, like checking whether the process exists and removing pid.txt if you're done.
Linux service init script are stored into /etc/init.d. You can copy and customize /etc/init.d/skeleton file, and then call
service [yourservice] start|stop|restart
see http://www.ralfebert.de/blog/java/debian_daemon/. Its for Debian (so, Ubuntu as well) but fit more distribution.
Maybe not the best dev-ops solution, but good for the general use of a server for a lan party or similar.
Use screen to run your server in and then detach before logging out, this will keep the process running, you can then re-attach at any point.
Workflow:
Start a screen: screen
Start your server: java -jar minecraft-server.jar
Detach by pressing: Ctl-a, d
Re-attach: screen -r
More info here: https://www.gnu.org/software/screen/manual/screen.html
Another alternative, which is also quite popular is the Java Service Wrapper. This is also quite popular around the OSS community.
Referring to Spring Boot application as a Service as well, I would go for the systemd version, since it's the easiest, least verbose, and best integrated into modern distros (and even the not-so-modern ones like CentOS 7.x).
The easiest way is to use supervisord. Please see full details here: http://supervisord.org/
More info:
https://askubuntu.com/questions/779830/running-an-executable-jar-file-when-the-system-starts/852485#852485
https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps
Here is a sample shell script (make sure you replace the MATH name with the name of the your application):
#!/bin/bash
### BEGIN INIT INFO
# Provides: MATH
# Required-Start: $java
# Required-Stop: $java
# Short-Description: Start and stop MATH service.
# Description: -
# Date-Creation: -
# Date-Last-Modification: -
# Author: -
### END INIT INFO
# Variables
PGREP=/usr/bin/pgrep
JAVA=/usr/bin/java
ZERO=0
# Start the MATH
start() {
echo "Starting MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "The service is already running"
else
#Run the jar file MATH service
$JAVA -jar /opt/MATH/MATH.jar > /dev/null 2>&1 &
#sleep time before the service verification
sleep 10
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Service was successfully started"
else
echo "Failed to start service"
fi
fi
echo
}
# Stop the MATH
stop() {
echo "Stopping MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
#Kill the pid of java with the service name
kill -9 $($PGREP -f MATH)
#Sleep time before the service verification
sleep 10
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Failed to stop service"
else
echo "Service was successfully stopped"
fi
else
echo "The service is already stopped"
fi
echo
}
# Verify the status of MATH
status() {
echo "Checking status of MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Service is running"
else
echo "Service is stopped"
fi
echo
}
# Main logic
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart|reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
exit 1
esac
exit 0
From Spring Boot application as a Service, I can recommend the Python-based supervisord application. See that stack overflow question for more information. It's really straightforward to set up.
Other answers do a good job giving custom scripts and setups depending on your platform. In addition to those, here are the mature, special purpose programs that I know of:
JSW from TanukiSoftware
YAJSW is an open source clone from the above. It is written in Java, and it is a nanny process that manages the child process (your code) according to configurations. Works on windows / linux.
JSVC is a native application. Its also a nanny process, but it invokes your child application through the JNI, rather than as a subprocess.
You can use Thrift server or JMX to communicate with your Java service.
From Spring Boot Reference Guide
Installation as an init.d service (System V)
Simply symlink the jar to init.d to support the standard start, stop, restart and status commands.
Assuming that you have a Spring Boot application installed in /var/myapp, to install a Spring Boot application as an init.d service simply create a symlink:
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
Once installed, you can start and stop the service in the usual way. For example, on a Debian based system:
$ service myapp start
If your application fails to start, check the log file written to /var/log/<appname>.log for errors.
Continue reading to know how to secure a deployed service.
After doing as written I've discovered that my service fails to start with this error message in logs: start-stop-daemon: unrecognized option --no-close. And I've managed to fix it by creating a config file /var/myapp/myapp.conf with the following content
USE_START_STOP_DAEMON=false
It is possible to run the war as a Linux service, and you may want to force in your pom.xml file before packaging, as some distros may not recognize in auto mode. To do it, add the following property inside of spring-boot-maven-plugin plugin.
<embeddedLaunchScriptProperties>
<mode>service</mode>
</embeddedLaunchScriptProperties>
Next, setup your init.d with:
ln -s myapp.war /etc/init.d/myapp
and you will be able to run
service myapp start|stop|restart
There are many other options that you can find in Spring Boot documentation, including Windows service.
Im having Netty java application and I want to run it as a service with systemd. Unfortunately application stops no matter of what Type I'm using. At the end I've wrapped java start in screen. Here are the config files:
service
[Unit]
Description=Netty service
After=network.target
[Service]
User=user
Type=forking
WorkingDirectory=/home/user/app
ExecStart=/home/user/app/start.sh
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
start
#!/bin/sh
/usr/bin/screen -L -dmS netty_app java -cp app.jar classPath
from that point you can use systemctl [start|stop|status] service.
To run Java code as daemon (service) you can write JNI based stub.
http://jnicookbook.owsiak.org/recipe-no-022/
for a sample code that is based on JNI. In this case you daemonize the code that was started as Java and main loop is executed in C. But it is also possible to put main, daemon's, service loop inside Java.
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo029
Have fun with JNI!
However once started I don't know how to access it to stop it
You can write a simple stop script that greps for your java process, extracts the PID and calls kill on it. It's not fancy, but it's straight forward.
Something like that may be of help as a start:
#!/bin/bash
PID = ps ax | grep "name of your app" | cut -d ' ' -f 1
kill $PID

Categories