Here are the commands I am familiar with that do what you may think I want:
For the sake of this question; "pn" = "process name"
pgrep pn
sudo /sbin/service pn status
ps -fC pn
The problem I am running into is Java. pgrep Java will give me the PID of all java processes. But if I know the exact name of a java application, let's call it coffee, pgrep Coffee gives me nothing. However, sudo /sbin/service Coffee status will give me the exact PID of Coffee. The problem is that I don't want and cannot use sudo in the script I am writing. So how do I find Coffee's PID even though it's a Java app?
I have the same problem. There might be better answers but what I do is this
ps -efw | grep pn
Then I can use awk to get just the column I want so that its just the pid.
ps -efw | grep pn | awk '{print $2}'
EDIT: Use the 'w' option in ps to get a wide output. Sometimes the name does not show up without the 'w' there.
w Wide output. Use this option twice for unlimited width.
Use this command
ps -ef| grep Coffee
You might want to look at the JPS (Java Process Status) tool. I can't remember if it comes with the runtime environment or not. It list out all currently running java processes, their PIDs, and the name of the Java application. You can probably parse the output to get the values you're looking for.
http://docs.oracle.com/javase/7/docs/technotes/tools/share/jps.html
Sample output:
jps
18027 Java2Demo.JAR
18032 jps
18005 jstat
Related
I am a newbie to linux. I am trying to find a command that displays
pid and java class.
I tried:
ps -C java -o pid
but it only showed me pid, not java classes.
So I used a different command:
ps -ef | grep java"
It showed me a full library of JAR files and associated java classes. However, I don't need all of that information, I need only to display pid and java class.
The problem that I am facing is that sometimes the java processes take like 100M of memory and I can only know the pid with the command java on it by using linux command:
top
But it couldn't find which java program is the one responsible for this. It will help me to debug that java file for memory leak.
ps aux | grep java
try this command and you can list of java process.
[or]
JPS will be helpful
JPS,JSTAT,JMAP,JSTACK,JHAT
you can run jps command it will show the pid value and based on these pid value others command will be executed.
Use java's jps command available from Sun Java.
I'm new to UNIX. I want to start my java app with a script like so:
#!/bin/sh
java -jar /usr/ScriptCheck.jar &
echo $! > /var/run/ScriptCheck.pid
This is supposedly working. It does run the app and it does write the pid file. But when I try to stop the process with a different script which contains this:
#!/bin/sh
kill -9 /var/run/ScriptCheck.pid
the console gives me this error:
bash: kill: /var/run/ScriptCheck.pid: arguments must be process or job IDs
My best guess is that I'm not writing the right code in the stop script, maybe not giving the right command to open the .pid file.
Any help will be very appreciated.
You're passing a file name as an argument to kill when it expects a (proces id) number, so just read the process id from that file and pass it to kill:
#!/bin/sh
PID=$(cat /var/run/ScriptCheck.pid)
kill -9 $PID
A quick and dirty method would be :
kill -9 $(cat /var/run/ScriptCheck.pid)
Your syntax is wrong, kill takes a process id, not a file. You also should not be using kill -9 unless you absolutely know what you are doing.
kill $(cat /var/run/ScriptCheck.pid)
or
xargs kill </var/run/ScriptCheck.pid
I think you need to read in the contents of the ScriptCheck.pid file (which I'm assuming has only one entry with the PID of the process in the first row).
#!/bin/sh
procID=0;
while read line
do
procID="$line";
done </var/run/ScriptCheck.pid
kill -9 procID
I've never had to create my own pid; your question was interesting.
Here is a bash code snippet I found:
#!/bin/bash
PROGRAM=/path/to/myprog
$PROGRAM &
PID=$!
echo $PID > /path/to/pid/file.pid
You would have to have root privileges to put your file.pid into /var/run --referenced by a lot of articles -- which is why daemons have root privileges.
In this case, you need to put your pid some agreed upon place, known to your start and stop scripts. You can use the fact a pid file exists, for example, not to allow a second identical process to run.
The $PROGRAM & puts the script into background "batch" mode.
If you want the program to hang around after your script exits, I suggest launching it with nohup, which means the program won't die, when your script logs out.
I just checked. The PID is returned with a nohup.
My goal is to list to STDOUT the class files and .jar files being executed by java on a Linux server. I could do some getopts thing to get args to -jar, but other processes identified by
ps -ef | grep java or ps -eo args | grep java
might be executing a class file, e.g. java -classpath /a/b/c myclass A1 A2 . I am concerned that I am looking at an inelegant solution full of lengthy piplines of greps and awk's to solve what should be (I think) a straightforward query. Given that:
some calls are made to just 'java' and others to the fully qualified pathname for java,
a variety of different (or no) java options may be set on the command line for running a process,
some processes call .jar files, some call .class files, and
there may be args to the class,
what is the best way to get a simple list of running java executables, like:
abc.jar
mymainclass
xyz.jar
numainclass
I think that this may be a not uncommon question, but I can't seem to build a search string that locates any previous discussion here. An elegant solution would be nice; right now I am looking at grepping '-jar' entries to a getops call, and parsing the remainder considering all possible combinations. I am working on a solution in bash 3.x
Thanks!
The jps command introduced in jdk5 might be what you are looking for. Using the -l and -m options it will output the pid main class and arguments. Adding -v will add the vm arguments.
This option lists all Java files currently opened by a java command. Maybe it is useful to you.
lsof | grep -E "^java.*(.jar|.class)$" | sed -E "s/\s+/\t/g" | cut -f9
It works in Debian.
I need some help writing a command that will be put into a .sh. I want to return the process id, which in the output below is 3678, but I'm having diffuclty because the process id changes everytime it gets restarted, so my code breaks
Output:
[root#server1 /usr/home/aaron]# ps -xauww | grep java | grep www
www 3678 0.0 3.2 1308176 267864 ?? Is 3:21PM 0:17.19 [java]
[root#server1 /usr/home/aaron]#
Heres what I was doing until I realized the column changed when the pid changed:
ps -xauww | grep java | grep www | cut -d" " -f6
Any help is appreciated. thanks.
If the starting is automated by a shell script, you can write the pid of the just-started-process which is in the variable $!.
java ...... &
echo "$!" > myjavaprogram.pid
When you need to kill it, just do:
kill `cat myjavaprogram.pid`
Below pgrep command works for getting pid by jar-file name:
pgrep -f test-app.jar
As per http://cfajohnson.com/shell/cus-faq-2.html
How do I get a process id given a process name? Or, how do I find
out if a process is still running, given a process ID?
There isn't a reliable way to to this portably in the shell. Some
systems reuse process ids much like file descriptors. That is,
they use the lowest numbered pid which is not currently in use
when starting a new process. That means that the pid you're
looking for is there, but might not refer to the process you think
it does.
The usual approach is to parse the output of ps, but that involves
a race condition, since the pid you find that way may not refer to
the same process when you actually do something with that
pid. There's no good way around that in a shell script though, so
be advised that you might be stepping into a trap.
One suggestion is to use pgrep if on Solaris, and 'ps h -o pid -C
$STRING' if not, and your ps supports that syntax, but neither of
those are perfect or ubiquitous.
The normal solution when writing C programs is to create a pid
file, and then lock it with fcntl(2). Then, if another program
wants to know if that program is really running, it can attempt to
gain a lock on the file. If the lock attempt fails, then it knows
the file is still running.
We don't have options in the shell like that, unless we can supply
a C program which can try the lock for the script. Even so, the
race condition described above still exists.
That being said look at this: http://www.faqs.org/faqs/unix-faq/faq/part3/section-10.html it might help you out ?
One way can be found in: man pgrep
I have a Java application executed from a ([ba]sh) shell script and unfortunately sometimes the people responsible for deploying it and starting it fail to switch to the appropriate user before starting the application. In this situation I'd like the application to not run at the very least, and ideally issue a warning not to do that. I thought about trying to alias java or change the path for root to include a fake java which does so, but this might have undesirable side effects and isn't going to be effective easily since the shell script specifies the full path to the java binary.
So, is there a standard idiom in shell scripts for 'don't run if I'm root'?
Example in bash:
if [ `id -u` = 0 ]; then
echo "You are root, go away!"
exit 1
fi
In BASH, you can take the output of whoami and compare it to root.
I use something like this at the beginning of scripts that I want to
be run under a service account:
LUSER='my-service'
if [ `id -un` != $LUSER ]; then
exec su $LUSER -s $SHELL -c "$0 $#"
fi
# actual script commands here.
If run as the correct user, execution will continue as planned. If run
as root, privileges are dropped to the wanted user-id. Other users
will get a password prompt which should tell them that something is
wrong.
su -s $SHELL ... is used to override the shell set in /etc/passwrd
-- it may be set to /bin/false for the service account.
I have used this on Debian systems, using bash and dash. Feel free
to comment if portability can be improved.