Video in progress

How do kill and kill -9 affect a Java process?

Plain kill sends SIGTERM: the JVM runs its shutdown hooks and exits cleanly. kill -9 is SIGKILL: the process is killed immediately, with no cleanup at all.

📖 Answer

pgrep -f shop-api.jar    # find the PID
kill 21347               # SIGTERM: please shut down
kill -9 21347            # SIGKILL: forced

When SIGTERM arrives, the JVM runs the shutdown hooks. Spring Boot uses this to close the context: @PreDestroy methods run, the connection pool is closed, and if configured, in-flight requests are allowed to finish.

A process can neither catch nor refuse SIGKILL. The result: a half-written file, unfinished requests, messages to the broker that were never acknowledged.

The right order is a plain kill first, wait a bit, and use -9 only if the process hangs. Kubernetes does the same: SIGTERM first, and SIGKILL after terminationGracePeriodSeconds (30 seconds by default) has passed.

Ctrl+C in a terminal sends SIGINT, which is also a clean-shutdown signal for the JVM.