How netbeans stops a run? - java

I'm programming with Java in Linux using Netbeans 7 and as my program (sometimes) could not exit (not in this eon, maybe) I create a thread to handle shutdown
Runtime.getRuntime().addShutdownHook(new StopThread());
But when I launch the code with netbeans (F6) and stop it through the "STOP" button the thread is not created; but if I run the program through the terminal and send CTRL-c the thread is created.
The question is: what type of signal netbeans launches to terminate the program?
Or (better): how can i handle the stop of netbeans such that the thread is created if i force to stop the program?
Or is there a way to modify how the stop works?
Thank you!

You are on Linux and this makes it really easy. I have written very little test and observe
the same behavior as you - with crtl+c running from the terminal hook is working, stopping in Netbeans it does not. Here are some workarounds:
I'm on Gnome and when I find the running process in the System Monitor and press "End process" on it - hook is working.
Other, more universal way:
$ ps ax | grep Hook
17144 ? Sl 1:21 /usr/lib/jvm/java-6-sun/bin/java -Dfile.encoding=UTF-8 -classpath /home/isopov/NetBeansProjects/ShutdownHookTest/build/classes:/home/isopov/NetBeansProjects/ShutdownHookTest/src shutdownhooktest.ShutdownHookTest
17176 pts/2 R+ 0:00 grep --color=auto Hook
$ kill -15 17144
Or in one line:
$ ps x | grep HookTest | grep java | awk '{print $1}' | xargs kill -15

Related

How can I kill a just started Java process in shell script?

I have this simple script where I execute a .jar file and I'd need its PID for killing it after the sleep command:
java -jar test.jar &> output.txt & pid=$! sleep 10
The problem is: the PID changes after the application gets fully launched, this pid I get in first place is not the same PID the application has after 10 seconds sleeping (I checked it using ps).
How can I track down the PID so that I can kill the fully launched application?
I've tried using pstree -p $pid but I get a long list of Java children processes and I thought there might be a better way to implement this other than getting every child process, extracting PID using grep and killing it, also because I'm not 100% sure this is working.
I found another solution using jps but I'd prefer use native linux commands for compatibility.
I don't necessarily need PID, using process name could be a way but I don't how to retrieve that either having only parent process' PID.
If you want to use process name, might run :
$ kill -9 $(ps aux | grep '[t]est.jar' | awk '{print $2}')
Options Details:
kill: sends a kill signal (SIGTERM 15: Termination) to terminate any process gracefully.
kill -9: sends a kill signal (SIGTERM 9:Kill) terminate any process immediately.
ps: listing all processes.
grep: filtering, [p] prevent actual grep process from showing up in ps results.
awk: gives the second field of each line, which is PID. (ps output : $1:user, $2:pid ...)
Two ways.
use system. Exit() inbuilt function.
or
redirect the pid number to a file and use later to kill the process.
Ex:-
java -jar test.jar &> output.txt & echo $! > pid-logs
cat pid-logs | xargs kill -9

How to stop the process from running?

This question might be a bit similar to this or this question.
How do I stop a particular .jar file from running in Mac OS? When I open up the activity monitor, it does not show the process listed. But I am sure it is running because when I visit the localhost (I have developed a .jar from a Spring Boot application), I can still see the welcome message!
Could you please let me know how I could stop a particular .jar file from executing? Thanks.
You can use jps to get the pid (and jar name), awk to parse the pid and then kill it. Like, (with bash or similar)
kill -9 $(jps | grep -i "thejar.jar" | awk '{print $1}')
or
kill -9 `jps | grep -i "thejar.jar" | awk '{print $1}'`
The -i option to grep makes it case insensitive. Omit if that is not needed.
Step1: ps -aux | grep xxx, xxx is the name of .jar
Step2: kill -9 pid, pid you can get from the first command.

Getting PID of a process that has just started

I am working on a Gradle Java project. Which starts Tomcat for testing and stops it later.
I am supposed to kill this Tomcat instance when the test fails.
I tried using "ps aux| grep tomcat | grep -v grap | awk {print $2}" command to get the process id and kill the process.
But on Production, there will be so many Tomcat processes running simultaneously by many users, I just want the tomcat process started by my build.gradle for test execution.
So how can I accomplish the task? Please provide me some guidelines.
You need to find a unique string in the output of 'ps aux' which differentiates your test tomcat and others'.
I currently use the below script to run 'shutdown.sh' first and then kill the PID as most of the times, the application stops but the process does not stop.
PID=`ps -ef | grep $JAVA_HOME/bin/java | grep "$TOMCAT_LOC"/conf | grep -v grep | awk '{ print $2 }'`
if [ $PID ]; then
echo tomcat is running with PID:$PID.
# Stop or Kill running Tomcat
if [[ -f $TOMCAT_LOC/bin/shutdown.sh ]]; then
[[ ! -x $TOMCAT_LOC/bin/shutdown.sh ]] && chmod a+x $TOMCAT_LOC/bin/shutdown.sh
$TOMCAT_LOC/bin/shutdown.sh >>/dev/null
sleep 20
fi
kill -9 $PID
sleep 3
else
echo tomcat is not running
fi
You may also look at configuring a PID file by editing the 'catalina.sh' which you can read later to find out your PID.
# CATALINA_PID (Optional) Path of the file which should contains the pid
# of the catalina startup java process, when start (fork) is
# used
Java JRE has tool called jps in $JAVA_HOME/bin folder.
It's similar to unix ps command but for java only.
You can use it to determined exac java process you need.
Using this tool is more recommended and actually it is more useful, when you have more than one java applications is running on your host...
for example I have running h2 database and many other apps, but wanna kill only h2, so I can use jps to get it PID
$ jps
17810 GradleDaemon
17798 GradleWrapperMain
17816 h2-1.4.197.jar
17817 GradleDaemon
17818 GradleDaemon
18011 Jps
16479
and then just kill needed process:
kill -9 17816
and all other java apps will continue work normally. I not sure about tomcat, but I think it can be done in similar way, something like that:
kill -9 $(jps | grep tomcat | awk '{print $1}')
Lastly, little bit offtopic, but a specially to your case: correct way would be using start/stop/restart scripts provided by tomcat
The correct way to terminate a Tomcat instance is via its own shutdown command. You should not be thinking of processes, or PIDs, or kills, at all.
so if you want to kill tomcat from that user from which you have logged in then try following and let me know if this helps you.
ps -ef | grep -v grep | grep `whoami` | grep tomcat
So by ps -ef I am listing all the processes then grep -v grep will remove the grep command's process then grep whoami will look for your currently logged in user then grep tomcat will look only for tomcat process, test it once and if All is Well then you could kill it.
By the way how about tomcat stop script? In case it is there you could use that also.
You can use shell variable $!. It represents the PID of the most recent background command.
yourCommand &
CMD_PID=$!
echo $CMD_PID

terminal - run java app in background and how to close it?

I have game server and I want to launch it on Asustor NAS Server. I have PuTTy and I write
nohup java -jar server.jar &
Good, it launch in background, but now I want to close it. How to do it? And how to know if app is running or not?
Normally when starting you get the pid returend like so:
~ $ nohup java -jar server.jar &
[1] 3305
~ $ nohup: ignoring input and appending output to ‘nohup.out’
to see if it is running you can issue
~ $ ps -ef | grep server
user1 3305 2936 0 13:58 pts/1 00:00:00 java -jar server.jar
if you see a line like the above it is running. You may also hava a look at the nohup.out file, which is written to the directory you started the server in, by using
tail nohup.out
to kill the process issue kill . Where pid is the process id, you either remembered, or will find out by looking at the second row of the "ps -ef | grep server" command, in our case 3305
kill 3305
kill without options tries to end the process gracefully. Read more abut kill and ps by using
man kill
and
man ps
respectively.
Try like this.
Get number of your job using:
jobs
then use fg command:
fg %job_num
When it will be on foreground you can press CTRL+C to kill the process.

How to stop Jetty webserver in Google App Engine

If you are doing local development I want to know how we can stop/kill Jetty webserver? I have to close my Eclipse IDE whenever I need to do that. Is there any other way to do it? Or we have to kill the process?
Thanks.
You should see Jetty instance(s) running in one of the Console. Just press the Red Button.
Jetty can be stopped in the following ways:
You can click the "red" button in the Eclipse console window
You can type "q" then hit enter in the console window (or "r" for restart) (This may only work for Run Jetty Run)
If both of those fail, then if you're on linux or osx, you can open a terminal window and type the following to find the process id of Jetty and kill it. The process id is the 2nd column shown in the results, and you'll need to be careful that you're not seeing some other process here as well:
ps aux | grep jetty | grep java
then run the following to kill the process
kill process_id_from_above_call
You can run the above 2 commands in one shot by doing this:
kill $(ps aux | grep jetty | grep java | awk '{print $2}')
but be sure you're matching Jetty before you do this, as you may kill something you don't want to kill if it doesn't match correctly ;-) Note that if it still isn't killed by the above, you can force it to be killed by doing this:
kill -9 $(ps aux | grep jetty | grep java | awk '{print $2}')

Categories

Resources