Getting execution time of a user process in Linux - java

How can I get the details of a process in Linux. I want the total execution time, memory map of a process. In this context the process will be a simple Java program. I only know the file name, not the process id. Any help will be appreciated

If the process is not already running, then time will print total execution time. There is both a bash builtin and a standalone command of that name, with somewhat different behaviour. But as your mention of memory maps suggests an already running process, I'll concentrate on that.
You can find a process by executable using ps -C java. If you want to know what that java binary is running, you can do things like this to find the pid:
ps -C java -o pid,time,cmd | awk '/foo\.jar/{print $1,$2}' | \
while read pid time; do
echo "PID $pid, TIME $time"
cat /proc/$pid/maps
echo ""
done
If you don't like the output format, replace the loop with something else.
Here is what this does:
-C java: Find java processes
-o pid,time,cmd: Print these fields
/foo\.jar/: Only take lines containing that regular expression
print $1,$2: Print pid and time columns
/proc/$pid/maps: Access proc file conatining the memory map

If you know only the program name, and not the process id, you first have to find out which processes (they can be many of them) are running that program.
You could use e.g. some of the below commands to find out, assuming the program name is progname:
ps auxw | grep progname
pidof progname
pgrep progname
(notice that if you are using a JVM to run some Java program the progname is always java which does not help much)
You might also use top or htop
Once you got an interesting pid, e.g. 1234, you could get more information about that process with e.g.:
ps -lw 1234
cat /proc/1234/status
cat /proc/1234/stat
cat /proc/1234/stack
cat /proc/1234/maps
cat /proc/1234/smaps
Look also in other files and directories of /proc/1234/ such as /proc/1234/fd/ and others.
The Linux kernel is telling information about processes thru /proc so you should learn more about it.

I'm planning to use some memory profiling tools such as valgrid.

Related

Restarting process on -XX:OnOutOfMemoryError using shell script gives: Unrecognized option: -9

I'm trying to restart process when OOME happens. Java binary is launched using two shell scripts, one of them imports other. I don't have any control of the first one but can modify the second one as I want.
This is a prototype what I'm trying to do:
First shell script test.sh:
#!/bin/sh
JAVA_OPTS="$JAVA_OPTS -Xmx10m"
. test1.sh
echo $JAVA_OPTS
java $JAVA_OPTS $es_params TestMemory
Second shell script test1.sh:
#!/bin/sh
pidfile="test.pid"
touch $pidfile
params="$parms -Dpidfile=$pidfile"
kill_command="kill -9 \$(cat $pidfile)"
dir=$( cd $(dirname $0) ; pwd -P )
path="$dir/$(basename $0)"
start_command="$path $#"
restart_command="$kill_command;sleep 2;$start_command"
JAVA_OPTS="$JAVA_OPTS -XX:OnOutOfMemoryError=\"$restart_command\""
Generally what it does is JAVA_OPTS is constructed inside test1.sh and then used to run Java binary, which just writes PID in pidfile and then creates OOME.
Problem happens during execution, java can't understand what is a parameter and what is a class to run. I think it might be a problem of quoting, I tried different ways to escape JAVA_OPTS, but without any result. I'm either getting:
Unrecognized option: -9
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Or
Error: Could not find or load main class "-XX:OnOutOfMemoryError=kill
If I just take a value of JAVA_OPTS and put it manually in test.sh it runs perfectly.
Any ideas how can I change test1.sh to make it work? I think I tried almost every possible way of putting double and single quotes, but without any success. Also if I put restart_command in restart.sh file and use it instead of the variable, it works fine.
After running set -x I saw that shell modifies every single space character to ' ' - adds ' on both sides. Escaping doesn't gives any result. Any idea how to avoid this? So final commend is:
+ java -Xmx10m '"-XX:OnOutOfMemoryError=kill' '$(cat' 'test.pid);sleep' '2;/Users/davidt/test/TestMemory/bin/test.sh' '")' -Des.pidfile=test.pid TestMemory
Update
I can run simplified command successfully
java "-XX:OnOutOfMemoryError=echo 'Ups'" $es_params TestMemory
But it seems a general problem, shell just hates spaces into variables I guess:
JAVA_OPTS="\"-XX:OnOutOfMemoryError=echo 'Ups'\""
set -x
java $JAVA_OPTS TestMemory
This script fails and the last line is interpreted as:
java '"-XX:OnOutOfMemoryError=echo' ''\''Ups'\''"' TestMemory
I tried different options to escape
This is a shell problem. Based on the evidence, I'd say that one of the ; characters ... and possibly some why space ... is being interpretted by the shell when you don't want / need this to happen.
If you run set -x in the shell before running the command that is trying to start the JVM, you will see the actual command that is being used.
It seems shell translates every single space to ' ',
Not exactly. The single quotes are inserted by the shell into the output you are getting from set -x. They simply indicating where the argument boundaries are. They are not really there ... and they are certainly NOT being passed to the java command.
Any idea how to [a]void it?
What you need to do is start from the (final) command that you are trying execute ...
java -Xmx10m -XX:OnOutOfMemoryError="kill NNNN;sleep 2;/Users/davidt/test/TestMemory/bin/test.sh" -Des.pidfile=test.pid TestMemory
... and work backwards, so that the shell variables, expansions and escaping give you what you need.
The other thing to note is that this:
java -Xmx10m -XX:OnOutOfMemoryError="kill $(cat test.pid); ..."
probably won't work. The kill $(cat test.pid) command is using shell syntax and requires shell functionality to interpolate the contents of the PID file. I doubt that the JVM is going to know what to do with that. (Or more accurately. It will do what you have literally told it to do, but that will not be what you want ...)
If you really need to interpolate the pid file content when the restart command is run as you appear to be trying to do, then suggest that turn the restart command into a free-standing shell script, and set the file mode so that it is executable. It will be simpler and a lot easier to get working.
As a general piece of advice, is is a bad idea to be too clever with shell scripts. The exact semantics of variable expansion and command parsing are rather tricky, and it is easy to get yourself really confused ... if you are trying to do this at multiple levels.
I ended up put the script I wanted to execute in a separate file and gave it as a parameter to JVM to execute when OOME happens.
echo "echo 'UPS'" >> oome_happened.sh
JAVA_OPTS="\"-XX:OnOutOfMemoryError='oome_happened.sh'\""
set -x
java $JAVA_OPTS TestMemory
Like #DaTval said, you should put the command in a script. The script should be someting like.
#!/bin/bash
kill -9 $PPID
Kill the caller of scripts.

Stop java file running in ssh

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

Shell script call command line application

I have a Java program which I'd like to call inside a linux shell script. The Java program takes a user input from the command line.
I read somewhere that I can use echo to mimic user input as follows:
java myProgram
echo 1000
echo
However this doesn't work for me, the program is still waiting for the user input. Is there something I'm doing wrong? I can't imagine this is a difficult task.
You can use echo, but in a pipeline.
echo 1000 | java myProgram
If you want to send a file, you can use cat:
cat file.txt | java myProgram
Why not just pass in the value as an argument
java myProgram 1000
I think you should find the process id of your java process and then write to its /proc directory.
Say the id of the Java process is 4321, then output to
/proc/4321/fd/0

How to know what class is running in Unix?

I have a class that is being executed through a script in unix (Solaris 9). Inside of the script the class is being run like this:
java -cp $CLASSPATH myPackage.component.MyMainClass $PROPERTIES_PATH/myMainClassProperties.properties
That shell needs to be monitored in order to see whether it works correctly or it fails, and it also is going to be run parallel with a different parameter file. So, my questions are:
How can I know what class is being executed if I use top. Is that possible?**
Is there a way to uniquely identify the class so it doesn't crash when running parallel?
Will it always be shown as myPackage.component.MyMainClass in the table of processes?
When I say parallely, I refer to something like:
java -cp $CLASSPATH myPackage.component.MyMainClass $PROPERTIES_PATH/myMainClassProperties.properties
and in another window/session/job
java -cp $CLASSPATH myPackage.component.MyMainClass $PROPERTIES_PATH/mySomeOtherProperties.properties
**When I say so, is because top shows something like this:
PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
8545 batman 47 4 10 190M 112M sleep 0:04:00 0.07% java
9022 joker 91 4 10 286M 211M sleep 0:01:00 0.09% java
You can let top tell you the complete command, that will include any arguments you pass to the VM.
Also ps will give you the complete command as well (with the matching arguments).
In my debian I can switch the displayed command with the c toggle (start top and hit c until it shows the whole command)
for ps I use the arguments -ef (but -f) alone should do for that situation.
You may want to read the output of man ps and man top
It was possible to get the info using ptree | grep java or ptree | grep MyMainClass
It lists a tree of the processes that are being executed as well as the command that is being runned in there for example:
13456 ksh
123476 java -cp java -cp /classpath/fullpath myPackage.component.MyMainClass /full/properties/path/myMainClassProperties.properties
The first line is a shell script, and inside it (next line) calls the java jar. This works for Solaris 9.
Thanks #Angelo-Neuschitzer for the heads up.

How to set Process Name for my own application

I have created a simple java networking program. I am using Fedora. whenever I want to see what the processes run on my system I found that for my application the process Name is java. I want give process name for my application. How to give process name.
Thanks
Sunil Kumar Sahoo
One way to change the process name of an application is to use a native launcher (or to copy the java/java.exe executable to another name).
Personally I've had good results with Launch4j
You could pass a java property to the jvm when you start the process then that should show up when running a ps -eaf and you could even do a ps -eaf|grep myprop to see if it's running.
so you start the app like this:
java -cp . com.whatever.MyApp -DMyAmazingProgram=true
then you should see the MyAmazingProgram=true in the ps output.
Another way would be to start your app from a bash script file e.g, startMyAmazingApp.sh then that should show up in the ps output until the process ends.
That script would have to not exit until the java process finished so you'd need to have a script a bit like this (rough guess):
#!/bin/bash
RESULT=`java -cp com.whatever.MyApp`
HTH

Categories

Resources