杰瑞科技汇

Linux下如何精准kill掉Java进程?

Of course. Killing a Java process on Linux is a common task. The best method depends on whether you need a graceful shutdown or an immediate termination.

Linux下如何精准kill掉Java进程?-图1
(图片来源网络,侵删)

Here’s a complete guide, from the simplest method to more advanced and safer approaches.

The Short Answer: The Two Main Commands

You'll primarily use two commands, pkill or kill, combined with signals.

  1. Graceful Shutdown (Recommended): This allows the Java application to clean up resources, finish transactions, and log a shutdown message.

    pkill -f "java"

    or

    Linux下如何精准kill掉Java进程?-图2
    (图片来源网络,侵删)
    kill <pid>
  2. Forceful Termination (Use as a last resort): This immediately kills the process. The Java application cannot perform any cleanup.

    pkill -9 -f "java"

    or

    kill -9 <pid>

Detailed Guide: Step-by-Step

Step 1: Find the Java Process ID (PID)

First, you need to identify the Process ID (PID) of the Java application you want to stop. The ps command is perfect for this.

Option A: List all Java processes This will show you all running Java processes on the system.

ps -ef | grep java

Example Output:

# The command itself appears in the output, so we grep for '[j]ava' to hide it
user1   12345 12342  5 10:30 pts/0    00:24:15 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -jar my-app.jar --spring.profiles.active=prod
user2   23456 12343  0 11:00 pts/1    00:00:05 /usr/lib/jvm/java-8-oracle/bin/java -Xmx512m -cp com.example.Main
user3   34567 34566 99 11:45 ?        00:15:30 java -Djava.library.path=/opt/someapp/lib -jar some-service.jar
  • The second column is the PID (e.g., 12345, 23456, 34567).
  • The command line shows you which application it is (e.g., my-app.jar, some-service.jar).

Option B: Find a specific Java process by name If you know the name of the JAR or class, you can make the search more specific.

# Find a process running a specific JAR file
ps -ef | grep "my-app.jar"
# Find a process running a specific class
ps -ef | grep "com.example.Main"

Pro Tip: Using pgrep pgrep is a more direct tool for finding PIDs.

# Find the PID of any java process
pgrep java
# Find the PID of a java process with a specific argument
pgrep -f "my-app.jar"

Step 2: Kill the Java Process

Now that you have the PID, you can use the kill command.

Method 1: Graceful Shutdown (The Right Way)

This is the recommended first step. It sends the SIGTERM signal (signal 15) to the process. This signal is a request for the process to terminate. Well-behaved Java applications (especially those built with frameworks like Spring Boot) will have a "shutdown hook" that executes when it receives this signal. This hook is used to gracefully stop the application.

kill <pid>

Example: If the PID from the previous step was 12345, you would run:

kill 12345

What happens next?

  • The JVM receives the SIGTERM signal.
  • It executes all registered shutdown hooks.
  • The application should stop accepting new requests.
  • It finishes processing any existing requests.
  • It closes database connections, file handles, and other resources.
  • It logs a shutdown message.
  • The process exits cleanly.

If the process is unresponsive or stuck, it might ignore this signal. After waiting a reasonable amount of time (e.g., 30-60 seconds), you can proceed to the forceful method.

Method 2: Forceful Termination (The Last Resort)

This method should only be used if the graceful shutdown fails. It sends the SIGKILL signal (signal 9) to the process. This signal cannot be ignored or caught by the application. The operating system immediately terminates the process, which can lead to:

  • Resource leaks: Unclosed database connections or file handles.
  • Data corruption: If the application was in the middle of writing to a file or database.
  • Inconsistent state: The application might be left in a partially updated state.
kill -9 <pid>

Example:

kill -9 12345

Advanced & Convenient Methods

Using pkill to Kill by Name

pkill is like kill, but it finds and kills processes by name (or other attributes) without you needing to manually find the PID first. This is extremely useful for scripting.

Graceful shutdown of all Java processes:

# The -f flag matches against the full command line, which is useful for Java
pkill -f "java"

Forceful termination of all Java processes:

pkill -9 -f "java"

Example: Kill a specific application If you want to kill only the process running my-app.jar:

# Graceful
pkill -f "my-app.jar"
# Forceful
pkill -9 -f "my-app.jar"

Using jcmd (JVM-specific command)

If you have the JDK installed, jcmd is a powerful tool that can send commands directly to a running JVM, often providing more control than a simple kill.

List all Java processes and their PIDs:

jcmd

Check if the JVM supports the "stop" command:

jcmd <pid> help

Look for a command named stop or ManagementAgent.stop.

Gracefully stop the JVM: This is often the most elegant way to stop a Java application, as it goes through the proper JVM shutdown sequence.

jcmd <pid> stop

Summary Table

Method Command Signal When to Use
Graceful Shutdown kill <pid> SIGTERM (15) Always start here. Allows the app to clean up.
Graceful (by name) pkill -f "java" SIGTERM (15) Quick way to gracefully kill all Java processes.
Forceful Kill kill -9 <pid> SIGKILL (9) Last resort. Use if graceful shutdown fails.
Forceful (by name) pkill -9 -f "java" SIGKILL (9) Quick way to force-kill all Java processes.
JVM Command jcmd <pid> stop N/A Best for controlled shutdown of a specific JVM.
分享:
扫描分享到社交APP
上一篇
下一篇