杰瑞科技汇

ps ef与grep java如何协同查看Java进程?

Of course! The command you're looking for is ps combined with grep. The ps command is used to list running processes, and grep is used to filter that list.

ps ef与grep java如何协同查看Java进程?-图1
(图片来源网络,侵删)

Let's break it down.

The Correct Command

The standard command to find all Java processes is:

ps -ef | grep java

Understanding the Command

Let's look at each part:

  1. ps -ef

    ps ef与grep java如何协同查看Java进程?-图2
    (图片来源网络,侵删)
    • ps: Stands for "process status". It's the command to display information about currently running processes.
    • -e: Selects all processes on the system. (This is equivalent to using the "A" flag).
    • -f: Displays a full listing. This provides more detailed information like the user who started the process, the process ID (PID), the parent process ID (PPID), the start time, and the full command line.
  2. (The Pipe)

    • This is a "pipe". It takes the output of the command on its left (ps -ef) and "pipes" it as the input to the command on its right (grep java). It's a way to chain commands together.
  3. grep java

    • grep: A powerful tool for searching text for lines that match a pattern.
    • java: The pattern it's searching for. It will look for the word "java" in the output it receives from ps -ef.

Example Output

When you run ps -ef | grep java, you'll see output similar to this:

# The actual output from ps -ef (a very small part of it)
UID        PID  PPID  C STIME TTY          TIME CMD
root      1234     1  0 10:30 pts/0    00:00:05 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -jar my-app.jar
user1     5678  1234  0 10:31 pts/1    00:00:02 java -cp .:lib/* com.example.MyMainClass
user2     9012  5000  0 11:00 ?        00:00:00 grep --color=auto java

Let's break down the columns you'll see:

Column Meaning Example Value
UID User ID of the process owner root, user1
PID Process ID (the unique identifier for the process) 1234
PPID Parent Process ID (the ID of the process that started this one) 1
C CPU utilization 0
STIME Start time of the process 10:30
TTY The terminal from which the process was started pts/0
TIME Total CPU time the process has used 00:00:05
CMD The full command line used to start the process /usr/lib/jvm/java-11-openjdk-amd64/bin/java -jar my-app.jar

Important Note: The last line (grep --color=auto java) is the grep command itself searching for its own name! This is normal. If you want to exclude the grep process from your results, see the advanced tip below.


Common Variations

Here are some other ways to achieve the same or similar results.

Using aux instead of ef

This is another very common way to list processes, especially on Linux systems.

ps aux | grep java
  • a: Show processes for all users.
  • u: Display the process in a user-oriented format (shows %CPU, %MEM, etc.).
  • x: Also show processes not attached to a terminal.

The output columns are slightly different but the goal is the same.

Using pgrep (The Modern, Simpler Way)

If you only need the Process ID (PID) and not all the other details, pgrep is a much cleaner tool.

pgrep java

This will simply output a list of PIDs, one per line:

1234
5678

You can use it to count the number of Java processes:

pgrep java | wc -l

Or to kill them all (use with caution!):

# The -x flag ensures the whole command name is matched
# The -u flag specifies the user to match
pkill -x -u user1 java

Advanced Tips

Excluding the grep Process

To filter out the grep command itself from the results, you can use the following trick. This works because grep excludes lines that match its own pattern.

ps -ef | grep java | grep -v grep
  • grep -v grep: The -v flag inverts the match, so it will show all lines that do not contain the word "grep".

Finding the Full Path to the Java Executable

Sometimes you need to know the exact path to the java binary being used. You can use ls -l on the PID's /proc filesystem.

# First, find the PID of the Java process you're interested in
ps -ef | grep java
# Let's say the PID is 1234. Now run this command:
ls -l /proc/1234/exe

This will output a symbolic link that points to the actual Java executable, for example: /proc/1234/exe -> /usr/lib/jvm/java-11-openjdk-amd64/bin/java

分享:
扫描分享到社交APP
上一篇
下一篇