Sobes.tech
Intern

How to terminate the process?

sobes.tech AI

Answer from AI

To terminate a process in Linux, you can use the commands kill, pkill, or killall.

1. Using kill:

  • Terminates a process by its Process ID (PID).
  • Requires knowing the PID of the process.
  • Signals:
    • SIGTERM (default, 15): Graceful termination, allows the process to exit properly.
    • SIGKILL (9): Immediate and forceful termination, cannot be ignored by the process.
# find the PID of the process
ps aux | grep <process_name>

# send SIGTERM signal (default)
kill <PID>

# send SIGKILL signal
kill -9 <PID>

2. Using pkill:

  • Terminates processes by name or other attributes.
  • Does not require knowing the PID.
  • Supports pattern matching.
# terminate all processes named 'nginx'
pkill nginx

# terminate all processes containing 'myapp' in their name (case-insensitive)
pkill -i myapp

# send SIGKILL to processes named 'firefox'
pkill -9 firefox

3. Using killall:

  • Terminates processes only by their exact name.
  • Does not support complex pattern matching like pkill.
# terminate all processes named 'apache2'
killall apache2

# send SIGKILL to processes named 'mysqld'
killall -9 mysqld

Choice of command:

Command Method of termination Requires PID Pattern support
kill By PID Yes No
pkill By name/attributes No Yes
killall By exact name No No

In most cases, it is recommended to first try terminating the process with SIGTERM (kill <PID>, pkill <name>) to allow it to save data and exit properly. SIGKILL (kill -9, pkill -9, killall -9) should be used only as a last resort when the process does not respond to other signals.