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.
Related
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
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)
I have this command in my deployment process. But I want this process to stop and then restart while deploying in linux server. I checked and found that this is simply a JAVA process, and I can't simply kill JAVA as other nimbus and supervisors are running too.
sudo nohup java -Droute=full -jar /opt/artifacts/project/project.jar --spring.config.location=/etc/project/application_full.properties >/dev/null 2>&1 &
So, how can I stop this process?
Your oneliner kill: (I know it's bad, but it should work)
ps -ef| grep "name_of_service" | grep -oP "root\s+(\d+)\s" | grep -oP "\d+" | kill
ps -ef finds the program line:
root 5727 1 0 11:38 ? 00:00:00 grep service
Then we use grep to remove parts we don't want.
And lastly pass the pid to kill.
ps: replace 'root' for the user you know run the service/pid you are looking for.
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
I'm trying to develop a bash build script for a Java project that will be run on Ubuntu and Fedora. Ubuntu uses the gcj compiler while Fedora uses IcedTea.
Both report their errors and warning in slightly different ways, and I want to ignore the warnings (I know, not generally a good idea, but some of the warnings are simply idiotic).
For gcj, I want to run:
javac *.java 2>&1 | grep -A 4 "error:"
but for IcedTea, I want to run:
javac *.java 2>&1 | grep -A 4 "error:\|errors\|.java:"
I'm still new to bash, so how would I write an if statement that would run one versus the other based upon the javac version?
Assuming your java and javac binaries match, and that icedtea is the special case.
#!/bin/bash
ERROR="error:"
java -version 2>&1 | grep -i icedtea > /dev/null
if [ $? -eq 0 ]; then
ERROR="error:\|errors\|.java:"
fi
javac *.java 2>&1 | grep -A 4 $ERROR
On my system, icedtea and sun have the same output for "javac -version", but not for "java -version".
Writing Java build scripts in bash (or any other shell language) has a number of problems:
scripts tend to be non-portable due to shell differences, different command locations, incompatible command options and so on ... even if you try to make the portable.
scripts cannot cope with dependencies (or at least not easily)
scripts cannot cope with recompiling only stuff that has changed
Instead, I suggest that you write a "build.xml" file and use the Ant build tool. Ant has the advantage of running on any build platform that runs Java, and of taking care of the vast majority of platform differences. It is sort of like a better "Make" designed specifically for building Java.
#!/bin/sh
JAVAC_VERSION="`java -version 2>&1 /dev/null | awk '/IcedTea/ {print $4}' | sed -e 's/[\(0-9]//g'`"
ICEDTEA="IcedTea"
if [ ${JAVAC_VERSION} = ${ICEDTEA} ]; then
javac *.java 2>&1 | grep -A 4 "error:\|errors\|.java:"
else
javac *.java 2>&1 | grep -A 4 "error:"
fi
exit 0
That should do it - if i understood your question correctly. How you get the version - im not quite sure of, but if my javac -version is incorrect just change it accordingly to your needs.