2.6.3. Killing processes
Table of Contents
Understanding Why and When to Kill Processes
In Linux, a process is a running program. Most of the time processes start, do their work, and exit cleanly. Sometimes, however, a process misbehaves. It might freeze, use too much CPU or memory, or keep a file or device busy when you need it released. In these situations you may need to ask the process to stop, or if that fails, forcefully terminate it.
Killing a process means sending it a signal that tells it to change its behavior or exit. You do not always have to use the most aggressive option. Often, you start with a gentle request and only escalate if the process ignores you.
Important rule: Try a graceful signal such as SIGTERM before using a forceful signal such as SIGKILL.
Signals and Process Termination
Linux uses signals as a simple way to communicate with processes. A signal is a small integer value that the kernel delivers to a process. Each signal has a name that begins with SIG. For killing processes, the most relevant signals are:
SIGTERM (signal 15) asks a process to terminate. The process can catch this signal and perform cleanup tasks. This is the default signal used by many tools when you "kill" a process.
SIGKILL (signal 9) immediately stops a process. The process does not get a chance to clean up. Files or data in memory may be left in an inconsistent state.
SIGINT (signal 2) is similar to pressing Ctrl + C in a terminal. It interrupts a process and asks it to stop.
In general, tools that "kill" processes let you choose which signal to send. If you do not specify a signal, the default is almost always SIGTERM.
Finding the Process to Kill
Before you can kill a process, you must identify it. There are several ways to do this. You might already know the process ID (PID), or you might only know the command name.
To list running processes, you can use tools such as ps or top. For example, to see processes for your user, you can run:
ps auxThis will display a table that includes the PID column and the COMMAND column. The PID is the number you need in order to send a signal to a specific process.
If you know part of the command name, there are convenience tools such as pgrep that can search for PIDs by name:
pgrep firefoxThis prints the PIDs of all processes whose names match "firefox". Once you have the PID, you can use it with commands that send signals.
Using `kill` by PID
The basic tool for sending a signal to a process is the kill command. Despite its name, kill sends any signal you choose, not just signals that terminate the process.
The general structure looks like this:
kill [options] PID
If you call kill with just a PID, it sends SIGTERM by default. This requests a clean shutdown:
kill 1234
This example sends SIGTERM to the process with PID 1234. If the process is responsive and written correctly, it should exit gracefully.
To specify a different signal, use the -SIGNAME form or -SIGNUMBER. For a forceful termination, you might use SIGKILL:
kill -SIGKILL 1234which is equivalent to:
kill -9 1234You can view available signals with:
kill -lThis prints a list of signal names and their corresponding numbers.
Important rule: Use kill PID first, and only if the process does not exit, consider kill -9 PID as a last resort.
Killing Processes by Name with `pkill`
Sometimes you do not want to look up PIDs manually. Instead, you can kill processes by matching their names. The pkill command does this. It searches for processes whose names match a pattern, then sends them a signal.
The basic form is:
pkill patternFor example, to request a graceful shutdown of all processes whose names match "firefox", you can run:
pkill firefox
This uses SIGTERM by default. To send a different signal, use the -signal option:
pkill -KILL firefox
That will send SIGKILL to all matching processes.
Because pkill can match multiple processes, you should use it carefully. Make your pattern specific enough to avoid stopping unrelated programs.
Killing Processes by Name with `killall`
Another tool for killing processes by name is killall. It behaves similarly to pkill, but its options and matching rules can differ slightly between Linux distributions.
A simple use looks like this:
killall firefox
This sends SIGTERM to all processes whose executable name is exactly firefox. To specify another signal, use the -s option or a direct -SIGNAL form:
killall -s SIGKILL firefoxor:
killall -KILL firefox
On some systems, killall has different behavior, so you should consult the manual page with man killall to understand the exact matching rules. Like pkill, it can affect multiple processes at once, which is convenient but potentially dangerous.
Forceful vs Graceful Termination
Killing a process is not always the same level of severity. A gentle signal gives the process a chance to save work and release resources. A forceful signal does not.
SIGTERM is considered a graceful signal. Many applications interpret it as a request to close. For example, they may save unsaved data or complete current operations.
SIGINT can also be considered a gentle way to stop a process that you control from the terminal, such as a command that is still running.
SIGKILL is the hard stop. The kernel immediately removes the process from memory. The process cannot ignore this signal and cannot catch it.
Important statement: SIGKILL (kill -9) should be used only when a process ignores gentler signals such as SIGTERM.
If a process is writing to a file or database when it is killed forcefully, that data may become corrupted. For system services and critical programs, this can cause side effects that are not immediately obvious.
Permissions and Ownership When Killing Processes
In Linux, permissions apply to processes as well as files. You are generally allowed to send signals only to processes that you own. That means processes running under your user account.
If you try to kill a process owned by another user, you will usually receive a "Permission denied" error. To send a signal to such a process, you need elevated privileges.
If your user has sudo access, you can run:
sudo kill 1234This sends the default signal as the superuser, which can target any process except a few critical ones that the kernel protects.
The same applies to tools like pkill and killall. To affect system wide services or processes started by the root user, you must use sudo:
sudo pkill nginxBe especially careful when killing processes as root, because you can easily stop essential system components.
Killing Foreground Processes from the Terminal
When you run a command in a terminal and it does not return, it becomes a foreground process attached to that terminal. You do not need to know its PID to interrupt it.
To interrupt a foreground process, press Ctrl + C. This sends SIGINT to the process group associated with the terminal. Many programs interpret this as a request to cancel the current operation and exit.
Some programs may ignore Ctrl + C. In that case, you can try Ctrl + \ (backslash), which sends SIGQUIT. If even that does not work, you can open another terminal and use kill with the appropriate PID.
Sometimes a program seems frozen, but it is just busy. Before killing it, consider whether it might be in the middle of a long running operation. Killing it might leave output or data incomplete.
Using `top` or Similar Tools to Kill Processes Interactively
Interactive monitoring tools such as top make it easier to find and kill resource hungry processes. When you run:
top
you see a frequently updated list of processes, sorted by CPU usage by default. Within top, there is an option to send a signal to a specific PID without closing top.
Typically, you press the k key inside top, then you are prompted for the PID and optionally the signal to send. If you just press Enter when asked for the signal, it uses the default, usually SIGTERM.
After sending the signal, you can watch the top display to see if the process disappears or its CPU usage drops. If it remains stuck, you may leave top and use kill -9 PID as a stronger option, if appropriate.
Other tools, such as htop, offer more user friendly interfaces with function keys and menus. The underlying idea is the same. You select a process and send it a signal.
Potential Risks and Best Practices
Killing processes is a powerful operation. If you terminate the wrong program, you might lose unsaved work, break active network connections, or even cause system instability.
There are some basic practices that can reduce the risk:
Confirm the PID and command name before sending a signal. Use ps, top, or similar tools to verify that you are targeting the correct process.
Prefer using SIGTERM or the default signal first. Give the process a chance to exit on its own.
Reserve SIGKILL for processes that are clearly stuck and do not respond to other signals.
When using tools that act on names such as pkill or killall, make sure your pattern is specific. For example, pkill python might kill several unrelated scripts at once. A more precise pattern, or using the exact PID, is safer.
Be especially careful when using sudo to kill processes owned by other users or system services. Stopping a key service can lead to unexpected issues.
Important rule: Always double check the target process before sending a kill signal, especially when using sudo or name based commands such as pkill and killall.
By understanding how signals work and how to choose the right tool and signal level, you can manage misbehaving processes safely and effectively from the Linux command line.
Views: 0
Where to Move
Move chapter:
- ☰ 1. PART I — Linux Fundamentals (Beginner)
- ☰ 1.1. What Is Linux?
- ☰ 1.1.1. What is an operating system
- ☰ 1.1.2. History of Unix and Linux
- ☰ 1.1.3. The Linux kernel
- ☰ 1.1.4. GNU and the Free Software Foundation
- ☰ 1.1.5. Open source philosophy
- ☰ 1.1.6. Where Linux is used today
- ☰ 1.1.7. Desktop vs server vs embedded Linux
-
- ☰ 1.2. Choosing a Linux Distribution
- ☰ 1.2.1. What is a distribution?
- ☰ 1.2.2. Package formats (deb, rpm, pacman)
- ☰ 1.2.3. Popular distributions
- ☰ 1.2.3.1. Ubuntu
- ☰ 1.2.3.2. Debian
- ☰ 1.2.3.3. Fedora
- ☰ 1.2.3.4. Arch Linux
- ☰ 1.2.3.5. openSUSE
-
- ☰ 1.2.4. Rolling vs fixed release
- ☰ 1.2.5. Choosing the right distribution
-
- ☰ 1.3. Installing Linux
- ☰ 1.3.1. Creating a bootable USB
- ☰ 1.3.2. Installing on real hardware
- ☰ 1.3.3. Dual booting with Windows
- ☰ 1.3.4. Installing in a virtual machine
- ☰ 1.3.5. Partitioning
- ☰ 1.3.5.1. `/` (root)
- ☰ 1.3.5.2. `/home`
- ☰ 1.3.5.3. Swap
-
- ☰ 1.3.6. First-time setup
-
- ☰ 1.4. First Steps After Installation
- ☰ 1.4.1. Desktop environment basics
- ☰ 1.4.2. Updating the system
- ☰ 1.4.3. Installing essential tools
- ☰ 1.4.4. Setting up drivers
- ☰ 1.4.5. Using the software center
-
- ☰ 1.5. Linux Filesystem Hierarchy
- ☰ 1.5.1. Overview of the root filesystem
- ☰ 1.5.2. Important directories
- ☰ 1.5.2.1. `/etc`
- ☰ 1.5.2.2. `/var`
- ☰ 1.5.2.3. `/usr`
- ☰ 1.5.2.4. `/tmp`
- ☰ 1.5.2.5. `/dev`
- ☰ 1.5.2.6. `/proc`
- ☰ 1.5.2.7. `/sys`
-
- ☰ 1.5.3. File types
- ☰ 1.5.4. Absolute vs relative paths
-
- ☰ 1.6. The Linux Graphical Environment
- ☰ 1.6.1. GNOME
- ☰ 1.6.2. KDE Plasma
- ☰ 1.6.3. XFCE
- ☰ 1.6.4. File managers
- ☰ 1.6.5. System settings
- ☰ 1.6.6. Workspaces and window management
-
-
- ☰ 2. PART II — Command Line Essentials
- ☰ 2.1. Terminal and Shell Basics
- ☰ 2.1.1. What is a shell?
- ☰ 2.1.2. Bash vs Zsh vs Fish
- ☰ 2.1.3. Command structure
- ☰ 2.1.4. Environment variables
- ☰ 2.1.5. Getting help
- ☰ 2.1.6. Command history and autocomplete
-
- ☰ 2.2. Files and Directories
- ☰ 2.2.1. Listing files
- ☰ 2.2.2. Navigating directories
- ☰ 2.2.3. File operations
- ☰ 2.2.4. Wildcards and globbing
- ☰ 2.2.5. Searching files
-
- ☰ 2.3. Viewing and Editing Text Files
- ☰ 2.3.1. Viewing tools
- ☰ 2.3.2. Editing with nano
- ☰ 2.3.3. Introduction to vim
- ☰ 2.3.4. Input/output redirection
-
- ☰ 2.4. Users, Permissions, and Ownership
- ☰ 2.4.1. Users and groups
- ☰ 2.4.2. Permission types (r, w, x)
- ☰ 2.4.3. Changing permissions
- ☰ 2.4.4. Changing ownership
- ☰ 2.4.5. Using sudo
-
- ☰ 2.5. Package Management
- ☰ 2.5.1. Software repositories
- ☰ 2.5.2. APT (Debian/Ubuntu)
- ☰ 2.5.3. DNF (Fedora/RHEL)
- ☰ 2.5.4. Pacman (Arch Linux)
- ☰ 2.5.5. Snap, Flatpak, AppImage
- ☰ 2.5.6. System upgrades
-
- ☰ 2.6. Working with Processes
- ☰ 2.6.1. What is a process?
- ☰ 2.6.2. Viewing processes
- ☰ 2.6.3. Killing processes
- ☰ 2.6.4. Foreground and background jobs
- ☰ 2.6.5. System services and daemons
-
- ☰ 2.7. Introduction to Shell Scripting
- ☰ 2.7.1. Writing your first script
- ☰ 2.7.2. Variables
- ☰ 2.7.3. Conditionals
- ☰ 2.7.4. Loops
- ☰ 2.7.5. Functions
- ☰ 2.7.6. Making scripts executable
-
-
- ☰ 3. PART III — Intermediate System Administration
- ☰ 3.1. Systemd and Service Management
- ☰ 3.1.1. What is systemd?
- ☰ 3.1.2. Managing services (systemctl)
- ☰ 3.1.3. Boot targets
- ☰ 3.1.4. System logs (journalctl)
- ☰ 3.1.5. Creating custom services
-
- ☰ 3.2. Storage and Filesystems
- ☰ 3.2.1. Devices and partitions
- ☰ 3.2.2. Filesystems (EXT4, XFS, Btrfs)
- ☰ 3.2.3. Mounting and unmounting
- ☰ 3.2.4. Disk usage tools
- ☰ 3.2.5. Archiving and compression (tar, gzip, xz)
-
- ☰ 3.3. Networking Fundamentals
- ☰ 3.3.1. IP addressing
- ☰ 3.3.2. Subnets and routing
- ☰ 3.3.3. DNS and hostnames
- ☰ 3.3.4. Networking tools (ip, ss, ping)
- ☰ 3.3.5. Editing network configuration files
-
- ☰ 3.4. User and Group Administration
- ☰ 3.4.1. User information files
- ☰ 3.4.2. Creating and removing users
- ☰ 3.4.3. Password policies
- ☰ 3.4.4. Managing groups
- ☰ 3.4.5. Shell configuration
-
- ☰ 3.5. System Monitoring
- ☰ 3.5.1. CPU and memory monitoring
- ☰ 3.5.2. Disk and I/O monitoring
- ☰ 3.5.3. Log files in /var/log
- ☰ 3.5.4. Boot performance
- ☰ 3.5.5. Monitoring running services
-
- ☰ 3.6. Backup and Restore
- ☰ 3.6.1. Backup strategies
- ☰ 3.6.2. Using rsync
- ☰ 3.6.3. Creating tar archives
- ☰ 3.6.4. Using snapshot systems
- ☰ 3.6.5. Automating backups
-
- ☰ 3.7. Basic System Security
- ☰ 3.7.1. Firewalls (UFW, firewalld)
- ☰ 3.7.2. Authentication policies
- ☰ 3.7.3. SSH security basics
- ☰ 3.7.4. SELinux and AppArmor overview
- ☰ 3.7.5. Intrusion detection basics
-
-
- ☰ 4. PART IV — Advanced Linux Skills
- ☰ 4.1. Advanced Shell Scripting
- ☰ 4.1.1. Script structure
- ☰ 4.1.2. Error handling
- ☰ 4.1.3. Debugging scripts
- ☰ 4.1.4. Scheduled tasks (cron)
-
- ☰ 4.2. The Linux Boot Process
- ☰ 4.2.1. BIOS vs UEFI
- ☰ 4.2.2. Understanding GRUB2
- ☰ 4.2.3. Kernel loading
- ☰ 4.2.4. initramfs
- ☰ 4.2.5. Troubleshooting boot issues
-
- ☰ 4.3. Kernel Management
- ☰ 4.3.1. What is the kernel?
- ☰ 4.3.2. Kernel modules
- ☰ 4.3.3. Kernel parameters
- ☰ 4.3.4. Compiling a custom kernel
- ☰ 4.3.5. Kernel versioning and updates
-
- ☰ 4.4. Advanced Filesystems and Storage
- ☰ 4.4.1. LVM concepts
- ☰ 4.4.2. Logical volumes
- ☰ 4.4.3. RAID levels
- ☰ 4.4.4. Full disk encryption (LUKS)
- ☰ 4.4.5. Snapshots and rollbacks
-
- ☰ 4.5. Network Services
- ☰ 4.5.1. SSH server configuration
- ☰ 4.5.2. NFS
- ☰ 4.5.3. Samba
- ☰ 4.5.4. FTP/SFTP
- ☰ 4.5.5. Firewalls in depth
-
- ☰ 4.6. Logging and Auditing
- ☰ 4.6.1. Systemd logging
- ☰ 4.6.2. Traditional logs in /var/log
- ☰ 4.6.3. Auditd
- ☰ 4.6.4. Log rotation
- ☰ 4.6.5. Creating custom logs
-
- ☰ 4.7. Virtualization and Containers
- ☰ 4.7.1. Virtual machines (KVM/QEMU)
- ☰ 4.7.2. Linux containers (LXC/LXD)
- ☰ 4.7.3. Docker fundamentals
- ☰ 4.7.4. Podman vs Docker
- ☰ 4.7.5. Container networking
-
-
- ☰ 5. PART V — Server Administration (Expert)
- ☰ 5.1. Web Servers
- ☰ 5.1.1. Apache basics
- ☰ 5.1.2. Nginx basics
- ☰ 5.1.3. Virtual hosts
- ☰ 5.1.4. SSL/HTTPS
- ☰ 5.1.5. Reverse proxy concepts
-
- ☰ 5.2. Database Servers
- ☰ 5.2.1. MySQL/MariaDB
- ☰ 5.2.2. PostgreSQL
- ☰ 5.2.3. Database backup strategies
- ☰ 5.2.4. Database hardening
-
- ☰ 5.3. Email Services
- ☰ 5.3.1. SMTP fundamentals
- ☰ 5.3.2. Configuring Postfix
- ☰ 5.3.3. IMAP/POP3 with Dovecot
- ☰ 5.3.4. Spam filtering systems
- ☰ 5.3.5. Email routing concepts
-
- ☰ 5.4. DNS and DHCP
- ☰ 5.4.1. Understanding DNS
- ☰ 5.4.2. Setting up BIND
- ☰ 5.4.3. DNS zones and records
- ☰ 5.4.4. DHCP server configuration
- ☰ 5.4.5. DNS troubleshooting
-
- ☰ 5.5. Load Balancing
- ☰ 5.5.1. HAProxy fundamentals
- ☰ 5.5.2. Nginx as a load balancer
- ☰ 5.5.3. Session persistence
- ☰ 5.5.4. High availability concepts
-
- ☰ 5.6. Clustering and High Availability
- ☰ 5.6.1. Failover principles
- ☰ 5.6.2. Pacemaker
- ☰ 5.6.3. Corosync
- ☰ 5.6.4. Cluster resource management
- ☰ 5.6.5. Distributed filesystems
-
-
- ☰ 6. PART VI — DevOps and Cloud
- ☰ 6.1. Linux for Developers
- ☰ 6.1.1. Compiler toolchains (gcc, make)
- ☰ 6.1.2. Git fundamentals
- ☰ 6.1.3. Debugging tools (gdb, strace)
- ☰ 6.1.4. Build automation (Makefile, CMake)
-
- ☰ 6.2. DevOps on Linux
- ☰ 6.2.1. CI/CD principles
- ☰ 6.2.2. YAML basics
- ☰ 6.2.3. GitHub Actions
- ☰ 6.2.4. GitLab CI/CD
- ☰ 6.2.5. Automated testing
-
- ☰ 6.3. Configuration Management
- ☰ 6.3.1. Ansible basics
- ☰ 6.3.2. Playbooks and inventories
- ☰ 6.3.3. Puppet overview
- ☰ 6.3.4. Chef overview
-
- ☰ 6.4. Infrastructure as Code
- ☰ 6.4.1. Terraform fundamentals
- ☰ 6.4.2. Providers and resources
- ☰ 6.4.3. Modules
- ☰ 6.4.4. Cloud provisioning with Terraform
-
- ☰ 6.5. Cloud Computing with Linux
- ☰ 6.5.1. Linux on AWS
- ☰ 6.5.2. Linux on Azure
- ☰ 6.5.3. Linux on GCP
- ☰ 6.5.4. Cloud networking basics
- ☰ 6.5.5. Scaling and monitoring
-
-
- ☰ 7. PART VII — Master Topics
- ☰ 7.1. Hardening a Linux System
- ☰ 7.1.1. Hardening principles
- ☰ 7.1.2. Kernel hardening
- ☰ 7.1.3. File integrity monitoring
- ☰ 7.1.4. Vulnerability scanning
-
- ☰ 7.2. Forensics and Incident Response
- ☰ 7.2.1. Collecting evidence
- ☰ 7.2.2. Log and file recovery
- ☰ 7.2.3. Analyzing suspicious processes
- ☰ 7.2.4. Incident response workflow
-
- ☰ 7.3. Performance Tuning
- ☰ 7.3.1. CPU tuning
- ☰ 7.3.2. Memory tuning
- ☰ 7.3.3. Disk and I/O optimization
- ☰ 7.3.4. Profiling tools
-
- ☰ 7.4. Writing Linux Tools
- ☰ 7.4.1. Bash-based tools
- ☰ 7.4.2. Python command-line tools
- ☰ 7.4.3. Writing tools in C
- ☰ 7.4.4. Using Linux system APIs
-
- ☰ 7.5. Linux Internals
- ☰ 7.5.1. Process lifecycle
- ☰ 7.5.2. Memory management
- ☰ 7.5.3. Signals and IPC
- ☰ 7.5.4. Namespaces
- ☰ 7.5.5. cgroups
-
- ☰ 7.6. Becoming a Linux Expert
- ☰ 7.6.1. Career paths
- ☰ 7.6.2. Certifications (RHCE, LFCS, LPIC)
- ☰ 7.6.3. Building a homelab
- ☰ 7.6.4. Contributing to open source
-
-
Comments
Please login to add a comment.
Don't have an account? Register now!