How to distinguish running java programs - java

Our server machine runs many java programes. And some of them are launched with the command "java -jar ***.jar". but sometimes I have to stop one to update the class files in it. the problem is how can i fingure out which program is the one i want to stop, or is there a tool I can use to find out the executable jar files' location.

You can use jps command.
$ jps -v
34370 Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home -Xms8m
34341 main -XX:+TieredCompilation -Xbootclasspath/a:/usr/local/Cellar/leiningen/2.5.0/libexec/leiningen-2.5.0-standalone.jar -Dfile.encoding=UTF-8 -Dmaven.wagon.http.ssl.easy=false -Dleiningen.original.pwd=/Users/ntalbs/js-workspace/synapeditor_mobile -Dleiningen.script=/usr/local/bin/lein
jps will display all java processes. The first column is OS pid. You can check the messages on console, then kill what you want. Perhaps you want to check jps document from Oracle.
Also, you can use ps and grep command.
$ ps -ef | grep java
or
$ ps -aux | grep java

Related

Why my java process on Linux OS did not show -classpath in java command when I input "ps -ef | grep java"?

I have this issue about java command when I input the following shell command "ps -ef | grep java".
And I got following commands which shows two java processes
the first process is like "java -Dxxx org.apache.hadoop.hdfs.server.namenode.NameNode" which does not specify the -classpath option. this command is confusing me a lot I don't know how it can find the class of "org.apache.hadoop.hdfs.server.namenode.NameNode" and other classes or jars that it depends on
the second process is like "java -Dxxx -classpath xxx.jar org.apache.hadoop.yarn.server.resourcemanager.ResourceManager" which make senses to me what a noraml java command should be like.
Can anyone help me to explain why the first command does not have the classpath?
//first process
root 4116 1 0 Jan30 ? 00:07:55 /root/jdk1.8.0_181/bin/java -Dproc_namenode -Xmx1000m -Djava.net.preferIPv4Stack=true -Dhadoop.log.dir=/root/hadoop-2.8.5/logs -Dhadoop.log.file=hadoop.log -Dhadoop.home.dir=/root/hadoop-2.8.5 -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,console -Djava.library.path=/root/hadoop-2.8.5/lib/native -Dhadoop.policy.file=hadoop-policy.xml -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Stack=true -Dhadoop.log.dir=/root/hadoop-2.8.5/logs -Dhadoop.log.file=hadoop-root-namenode-hdfs01.log -Dhadoop.home.dir=/root/hadoop-2.8.5 -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,RFA -Djava.library.path=/root/hadoop-2.8.5/lib/native -Dhadoop.policy.file=hadoop-policy.xml -Djava.net.preferIPv4Stack=true -Dhadoop.security.logger=INFO,RFAS -Dhdfs.audit.logger=INFO,NullAppender -Dhadoop.security.logger=INFO,RFAS -Dhdfs.audit.logger=INFO,NullAppender -Dhadoop.security.logger=INFO,RFAS -Dhdfs.audit.logger=INFO,NullAppender -Dhadoop.security.logger=INFO,RFAS org.apache.hadoop.hdfs.server.namenode.NameNode
//second process
root 4595 1 0 Jan30 pts/0 00:28:38 /root/jdk1.8.0_181/bin/java -Dproc_resourcemanager -Xmx1000m -Dhadoop.log.dir=/root/hadoop-2.8.5/logs -Dyarn.log.dir=/root/hadoop-2.8.5/logs -Dhadoop.log.file=yarn-root-resourcemanager-hdfs01.log -Dyarn.log.file=yarn-root-resourcemanager-hdfs01.log -Dyarn.home.dir= -Dyarn.id.str=root -Dhadoop.root.logger=INFO,RFA -Dyarn.root.logger=INFO,RFA -Djava.library.path=/root/hadoop-2.8.5/lib/native -Dyarn.policy.file=hadoop-policy.xml -Dhadoop.log.dir=/root/hadoop-2.8.5/logs -Dyarn.log.dir=/root/hadoop-2.8.5/logs -Dhadoop.log.file=yarn-root-resourcemanager-hdfs01.log -Dyarn.log.file=yarn-root-resourcemanager-hdfs01.log -Dyarn.home.dir=/root/hadoop-2.8.5 -Dhadoop.home.dir=/root/hadoop-2.8.5 -Dhadoop.root.logger=INFO,RFA -Dyarn.root.logger=INFO,RFA -Djava.library.path=/root/hadoop-2.8.5/lib/native -classpath /root/hadoop-2.8.5/etc/hadoop:/root/hadoop-2.8.5/etc/hadoop:/root/hadoop-2.8.5/etc/hadoop:/root/hadoop-2.8.5/share/hadoop/common/lib/*:/root/hadoop-2.8.5/share/hadoop/common/*:/root/hadoop-2.8.5/share/hadoop/hdfs:/root/hadoop-2.8.5/share/hadoop/hdfs/lib/*:/root/hadoop-2.8.5/share/hadoop/hdfs/*:/root/hadoop-2.8.5/share/hadoop/yarn/lib/*:/root/hadoop-2.8.5/share/hadoop/yarn/*:/root/hadoop-2.8.5/share/hadoop/mapreduce/lib/*:/root/hadoop-2.8.5/share/hadoop/mapreduce/*:/root/hadoop-2.8.5/contrib/capacity-scheduler/*.jar:/root/hadoop-2.8.5/contrib/capacity-scheduler/*.jar:/root/hadoop-2.8.5/contrib/capacity-scheduler/*.jar:/root/hadoop-2.8.5/share/hadoop/yarn/*:/root/hadoop-2.8.5/share/hadoop/yarn/lib/*:/root/hadoop-2.8.5/etc/hadoop/rm-config/log4j.properties org.apache.hadoop.yarn.server.resourcemanager.ResourceManager
The scripts run export CLASSPATH=$(hadoop classpath) or look under $HADOOP_HOME for jars.
-classpath isn't required Java parameter
You can also use jps to see running Java processes, which is what the Hadoop documentation suggests

Unable to run nohup command from jenkins as a background process

UPDATE: Based on below discussion I have edited my answer for more accurate description.
I am trying to run a nohup command from jenkins. The full command is
nohup java -jar /home/.../jar/server-process-0.35.jar prod >> /var/../server-process-prod.log 2>&1 &
This command does not work. I can see status as success in jenkins but no java process in linux. When I do 'ps -ef | grep java'
However when I remove the last '&' , that is I change it from run in forground instead of background
It starts working. I can see the java process started.
The original command works fine If I run it on linux console.
I need to run it from jenkins in the original form that is as a backgorund process. So that it is independant of jenkins.
Any clues why is this happening?
Long story short, Jenkins kills all processes spawned by a job once that job finishes. To override this behavior, you need to set an environment variable.
The variable appears to vary from job type to job type. It used to be BUILD_ID, but for Pipeline jobs it is JENKINS_NODE_COOKIE, and there are several others mentioned in this answer.
So if you're running your command in Pipeline, it would look like this:
sh 'JENKINS_NODE_COOKIE=dontKillMe nohup java -jar /home/.../jar/server-process-0.35.jar prod >> /var/../server-process-prod.log 2>&1 &'
See the wiki on ProcessTreeKiller and this comment in the Jenkins Jira for more information.
In your jenkins shell script try:
export BUILD_ID=dontKillMe
nohup java -jar your_java_app.jar &
It worked for me!
I tried every possible combination with BUILD_ID but it didn't work.
I made it though by putting "nohup command > output.txt&" inside a shell script ran by the execute shell in jenkins, it worked perfectly!
Got the same problem, added:
BUILD_ID=dontKillMe python /var/lib/jenkins/release.py
into Execute Shell -> Command and inside release.py there is:
os.system('nohup java -jar ' + new_jars_on_server + '/' + generated_jar_by_mvn_name + '&')
and it works
Best simple solution is to use "at now" instead of "nohup"
In your job jenkins (execute shell) put :
set +e #so "at now" will run even if java -jar fails
#Run java app in background
echo "java -jar $(ls | grep *.jar | head -n 1)" | at now + 1 min
what worked for me was wrapping the nohup java -jar ... command into sh file inside execute shell command, and running that same sh file right after:
echo "starting java jar..."
cd [some location where jar is]
echo "nohup java -jar [jar_name].jar &" > start-jar-in-background.sh
sh start-jar-in-background.sh
echo "started java jar"
If I had nohup java -jar ... inline with Execute shell command, then it didn't start it from some reasons. I spent quite some time on this, hope it helps to someone ';)
Simplest way :
`nohup java -jar [jar_name].jar >log_file_you_want 2>another_file`&
set +e #so "at now" will run even if java -jar fails
#Run java app in background
echo "java -jar $(ls | grep *.jar | head -n 1)" | at now + 1 min
above command worked for him, thanks #walid, & remove at the end (+ 1 min)

Get Command Line of a java process

As we all know all java processes appear in task manager as javaw.exe. I want to get command line of all such java processes. A tool called ProcessExplorer does the same thing but it displays all that in a GUI and i am looking at a programming solution for the same.
Here is a snapshot from ProcessExplorer tool.
Any ideas??
You can get it with WMIC
C:\> wmic process where(name="javaw.exe") get commandline
If a command line solution helps and you have Windows Powershell available, you can use something like
PS> $c = Get-WmiObject Win32_Process -Filter "name='javaw.exe'" | select-Object CommandLine
PS> $x.CommandLine
"C:\Program Files (x86)\Java\jre7\bin\java.exe" "-Djava.class.path=C:\\PROGRA~2\\Java\\jre7\\classes" ...
use the command jps to find java processes
http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jps.html
With command prompt in windows, you can run below command
wmic process where(name="javaw.exe") get commandline
Sample Output with Command Prompt
CommandLine
"C:\Users\Public\agent\jre\bin\javaw.exe" -cp .\agent-jar-with-dependencies.jar com.agent.Runner
With powershell prompt, you can run below command
Get-WmiObject -Class Win32_Process -Filter "Name='javaw.exe'" | select-Object CommandLine
Sample Output with Powershell command
CommandLine
-----------
"C:\Users\Public\agent\jre\bin\javaw.exe" -cp .\agent-jar-with-dependencies.jar com.agent.Runner

How to create a script to restart a JAR file and get output in a terminal?

I have used the following scripting for start and stop a jar file.
**start.sh**
#!/bin/bash
nohup nice java -jar Server.jar > ./Server.out 2>&1 &
**stop.sh**
#!/bin/bash
kill `ps -ef | grep Server.jar | grep -v grep | awk '{ print $2 }'`
Now I want to merge both scripts and create a new restart script. I also want this script output in a terminal instead of a text file(Server.out).
Would appreciate any kind of input/help.
You can either put the commands of the two sripts after each other (kill first, java second) or just call the two scipts in the appropriate order.
The idea is that restart is basically equivalent to killing the current running version and starting a new one.
To avoid the output to a file, remove the > ./Server.out part.
Edit: removed note about removing the redirection part as I misread the grep part of the kill script
Update: Missed the nohup part of the script: with nohup you need to redirect output to a file, because the process is detached from the terminal (see documentation). If you do want to see the output in the terminal, remove nohup as well as the redirection to the file

How to change/assign process name of java .jar

I'm running Minecraft under Linux, which involves running an executable .jar file. This means it shows up as "java" under ps, rather than "minecraft". I would like to assign it the process name "minecraft".
Looking around, I found the following tip for assigning a process name via bash:
how to change the name of a Java application process?
exec -a goodname java ...
I usually run with:
java -cp ~/Games/Minecraft/Minecraft.jar net.minecraft.LauncherFrame
So tried make a bash script:
#!/bin/bash
exec -a minecraft java -cp ~/Games/Minecraft/Minecraft.jar net.minecraft.LauncherFrame
But when I run this, it still shows up as "java" under the ps command.
What am I doing wrong?
It works for me. I haven't tested with java, but I tested with sleep:
victor#vz:~$ exec -a minecraft sleep 1m &
[1] 3858
victor#vz:~$ ps x | grep mine
3858 pts/2 S 0:00 minecraft 1m
3860 pts/2 S+ 0:00 grep --color=auto mine
victor#vz:~$
However, this seems to be merely a cosmetic change as far as I can tell by the documentation:
victor#vz:~$ help exec exec: exec
[-cl] [-a name] [command [arguments
...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
Options:
-a name pass NAME as the zeroth argument to COMMAND
In reference to OP's comment to this answer: I just tested it on a remote machine with java as well:
victorz#exa:~$ javac test.java # spits out an Administrator.class file among others
victorz#exa:~$ exec -a minecraft java Administrator &
[1] 13142
victorz#exa:~$ ps x | grep mine
13142 pts/1 Sl 0:00 minecraft Administrator
13161 pts/1 S+ 0:00 grep --color=auto mine
victorz#exa:~$
Maybe you are not using the x switch to ps? I get no match unless I use the x switch.

Categories