Stop java file running in ssh - java

Im running a jar file as part of a large web app. The majority of the app is written in php, but there is one large .jar file that it interacts with. To start this jar file I use ssh to connect to the server, navigate to the directory and run it by calling:
java -jar file_name.jar
If I want to turn off this file, what's the ssh command for that ?

While agreeing with other comments and answers, I'd like to point out the oft forgotten jps tool packaged with JDK's
anders#localhost:~$ jps -v
15688 Jps -Dapplication.home=/usr/lib/jvm/java-7-oracle -Xms8m
which lists all running Java processes on the host (might want to sudo if the process wasn't started by your login user).
So, with some command line magic such as
kill -9 `jps -v | grep file_name.jar | awk {'print $1'}`
you would achieve your stated purpose.
Cheers,

If you do:
ps aux
or something similar (see man ps for the many different possible commands) you should be able to find the PID of the java process (might be difficult if there are many java processes running*).
Then do:
kill PID
If that doesn't work, try:
kill -9 PID
But this will not give the process a chance to shut down cleanly.
*) The reason this might be difficult with many java processes running, is that on some OS's, Java versions, etc, the process name might simply be "java", which makes it hard to distinguish them.
Update: Or you can use pgrep -lf file_name.jar to get the PID easier.
See https://linux.die.net/man/1/pgrep

Related

Why is jstack unable to open socket file when trying to generate a thread dump?

I am trying to generate a thread dump of a java process being run on a Linux instance in AWS. I am using the jstack command on OpenJDK version 1.8.0. The current command I am running is sudo -u <user> jstack -l <java pid> where <user> is the user that started the JVM.
When I run this, I receive the error Unable to open socket file: target process not responding or HotSpot VM not loaded
Potential Problem:
While reading about how jstack works, I noticed that jstack is supposed to generate a socket file /tmp/.java_pidXXX in order to attach to the process. This file is not generated.
My potential solution is that if I can get the socket file to generate, hopefully jstack will be able to run properly.
I am unsure why this error is occurring, but my only idea is that this could this be some kind of permissions error to create files in the /tmp directory. I tried testing my permissions by creating text files in the /tmp directory and I was able to create text files.
How can I get this socket file to generate? Any potential solutions would be greatly appreciated.
Edit
Here I have added the command that was used to create the JVM. The command used to get this command was ps -aux | grep java
java -server -Xmx8192m -XX:+HeapDumpOnOutOfMemoryError ->XX:HeapDumpPath="/tmp" -XX:MaxPermSize=256M -Djava.awt.headless=true ->Dsling.run.modes=dynamicmedia_scene7,,
<instance_name>,samplecontent,crx3,crx3ta>r -Djava.locale.providers=CLDR,JRE,SPI -jar crx-quickstart/app/cq-?>quickstart-6.5.0-standalone-quickstart.jar start -c crx-quickstart -i >launchpad -p 4502 -Dsling.properties=conf/sling.properties
Solution:
An update for anyone who comes across this in the future. I found a solution that worked for me by changing the command that I was using to initially start the JVM. I added the flag -XX:+StartAttachListener which forces the process to generate the /tmp/.java_pidXXX socket file during the booting of the JVM.
Other tips I came across in my journey to finding this solution:
Make sure the user executing the jstack command is the same user that who ran the process that you are trying to take the thread dump of.
Also, make sure the socket file /tmp/.java_pidXXX is not being automatically cleaned up by any background cleanup processes in the /tmp directory.

How to know if a jar file is already running?

After a research in google i found good answers like:
1)using jps or jps -l to get the jars running under JVM
OK with this answer but if the user has not java installed at all and i run my jar using for example a .bat file and a folder with java JRE.
Also gps function is experimental and only JDK contais it and not JRE.
2)Check if jar running from shell
I need a solution for this on windows platform.Although a platform indepedent solution is always prefferable.
Something more about jps(cause it is Platform Independent) I will appreciate an answer where you provide me a good solution with jps function.
use this command to check the jar is running or not.
ps aux | grep java
eg:
sys_name 7526 60.1 2.6 4474364 104092 pts/4 Sl+ 23:57 0:09 java -jar start.jar
You can us ps and grep on a *nix system as described above. For windows you can do:
tasklist /v /FI "IMAGENAME eq java.exe"
This will get you a list of all the Java programs running. I don't think you can get much closer on Windows.
To test this, pick a .exe file you see running in Task Manager and test out the cmd line.
If you see the following output :
No tasks are running which match the specified criteria
That means there is no task running equal to java.exe.
Use the following command:
ps aux | grep java

How to open java program in other linux computer without terminal holding?

I have written a java program with jar file. The java program is to update status of linux server so it need to keep running, but the linux server is in data center, so I need to remote to server to open the program. I use ssh to login linux server. Use command of "java -jar file.jar" to run the program.
However, the java program of the linux server will close if I close the terminal in my computer. Since I cannot keep opening my computer, I wanna know how to open the java programming without holding my computer terminal.
you need to use nohup to keep the program running after you log out.:
server:~name$> nohup java -jar file.jar &
this will keep your program running
Two ways
One
nohup java -jar file.jar &
Another
java -jar file.jar &
In both cases your process will go in background however the process will terminate in the second approach when shell terminates in second case.
If this program is intended to be running on all your machines for monitoring purposes, you should be running it as a service from your server's init system (systemd for most systems these days). You can use the Java Service Wrapper or jsvc or write your own init script.
Another solution apart from the proposed one:
screen -d -m java -jar your.jar
You will then have a detached screen with your java command in it. List with screen -l, reattach with screen -D -RR <screenid_obtained_via_screen_-ls>

How To stop an Executed Jar file

It feels like a dumb question to ask, but i cant seem to figure it out. when i run a *.jar file on windows it doesnt apears in the taskmanager processes. how can i terminate it , i have tried TASKKILL but it also doesnt work for me.
On Linux
ps -ef | grep java
It will show u a list of processes out of which one will be your executable jar. Just kill that process by its process id.
sudo kill -9 <pid>
Is there any way to do this from the java code of the same jar file. Like killing itself once process completed.
Find the process id by jps command & and kill them by taskkill command.
Note that "-f" is required with taskkill or it may just send a termination signal not actually terminating it.
You can identify the process in taskmanager by looking for "java" or "javaw" processes. The problem will be in case you are running more than one java processes. If you are able to identify your process, simply kill/end it.
Other way around:
Run
jps -lv
which shows PIDs and command lines of all running Java processes. Determine PID of the task you want to kill. Then use command:
taskkill /PID <pid>
to kill the your jar process.
Did you try to kill the java.exe processes in the taskmanager? It should stop then.
you could open jvisualvm to see the running java-processes. the process-id is displayed there. now open the task-manager go to the processes tab and add the process-id column to be displayed. now you can select the right java.exe or javaw.exe to kill
As everyone stated it is either java or javaw process. The problem is when you're running multiple apps like that. One workaround might be naming the process differently as stated in:
How can I set the process name for a Java-program?
spring boot start/stop sample (on Windows OS).
start.bat
#ECHO OFF
call run.bat start
stop.bat:
#ECHO OFF
call run.bat stop
run.bat
#ECHO OFF
IF "%1"=="start" (
ECHO start your app name
start "yourappname" java -jar -Dspring.profiles.active=prod yourappname-0.0.1.jar
) ELSE IF "%1"=="stop" (
ECHO stop your app name
TASKKILL /FI "WINDOWTITLE eq yourappname"
) ELSE (
ECHO please, use "run.bat start" or "run.bat stop"
)
pause
If you run the JAR file By command line and it is running yet. Press,
In Windows:
• Ctrl+C: shuts down the process, (it might be needed administrator privilege)
In Linux:
• Ctrl+ C : politely ask the process to shut down now.
• Ctrl+ \ : mercilessly kill the process that is currently in the foregroun.
In windows task manager you will see process called "java.exe". Kill that process your application will get stop.
To know the process first go to applications in task manager and then go to process by selecting that application. It will lead you to exact process of that application.
Regards,
Jaynil
if you are using a jframe and you want your application to stop when you click the "X":
here's a tutorial: http://tips4java.wordpress.com/2009/05/01/closing-an-application/
This is probably the easiest way to kill the process with no external dependencies (jps or anything).
wmic Path win32_process Where "CommandLine Like '%YourJarName.jar%'" Call Terminate
via How can we stop a running java process through Windows cmd?

How to find the process id of a running Java process on Windows? And how to kill the process alone?

I want to kill the particular Java process in Windows, like in Linux (ps -aux to get processid and then kill processid to kill the process).
You can use the jps utility that is included in the JDK to find the process id of a Java process. The output will show you the name of the executable JAR file or the name of the main class.
Then use the Windows task manager to terminate the process. If you want to do it on the command line, use
TASKKILL /PID %PID%
You can also find the PID of a java program with the task manager. You enable the PID and Command Line columns View -> Select Columns and are then able to find the right process.
Your result will be something like this :
This will work even when there are multiple instance of jar is running
wmic Path win32_process Where "CommandLine Like '%yourname.jar%'" Call Terminate
After setting the path of your jdk use JPS.Then You can eaisly kill it by Task ManagerJPS will give you all java processes
The solution I found is very simple. Use Window's WMIC & Java's Runtime to locate & kill the process.
Part 1: You need to put some sort of identifier into your app's startup command line. E.g. something like:
String id = "com.domain.app";
Part 2: When you run your app, make sure to include the string. Let's say you start it from within Java, do the following:
Runtime.getRuntime().exec(
"C:\...\javaw.exe -cp ... -Dwhatever=" + id + " com.domain.app.Main"
);
Part 3: To kill the process, use Window's WMIC. Just make sure you app was started containing your id from above:
Runtime.getRuntime().exec(
"wmic process Where \"CommandLine Like '%" + id + "%'\" Call Terminate"
);
In windows, we can use the PowerShell to list the java running process. Then using the process id we can kill the process. Please find the below commands that needs to be executed in the PowerShell.
To list the Java Process.
ps | Where-Object -Property ProcessName -EQ -Value 'Java'
To kill the java process with specific id.
Stop-Process <PID>
The above approach worked for me.
In windows XP and later, there's a command: tasklist that lists all process id's.
For killing a process in Windows, see:
Really killing a process in Windows | Stack Overflow
You can execute OS-commands in Java by:
Runtime.getRuntime().exec("your command here");
If you need to handle the output of a command, see example: using Runtime.exec() in Java
This is specific to Windows.
I was facing the same issue where I have to kill one specific java program using taskkill. When I run the java program, tasklist was showing the same program with Image name set as java.exe.
But killing it using taskkill /F java.exe will stop all other java applications other than intended one which is not required.
So I run the same java program using:
start "MyProgramName" java java-program..
Here start command will open a new window and run the java program with window's title set to MyProgramName.
Now to kill this java-program use the following taskkill command:
taskkill /fi "MyProgramName"
Your Java program will be killed only. Rest will be unaffected.
Open Git Bash
Type ps -ef | grep java
Find the pid of running jdk
kill -9 [pid]

Categories

Resources