Skip to content
Log In Sign Up info@muvcloud.com

The Short Answer

pgrep -a myscript
kill 4182

pgrep -a lists the matching processes with their PID and full command line. kill 4182 sends SIGTERM to PID 4182. Use sudo for processes that belong to another user or to root.

Find the Process ID

pgrep -a nginx
ps aux | grep '[n]ginx'
pidof nginx

The brackets in the grep pattern keep grep from listing itself. In top and htop the PID is the first column. More ways are in how to list processes in Linux.

The Signals You Will Use

SignalNumberEffect
SIGTERM15Asks the program to exit cleanly; the default
SIGINT2The same as Ctrl+C
SIGHUP1Many daemons reload their config
SIGKILL9Ends the process at once; it cannot clean up

kill -l lists every signal. You can name a signal or give its number: kill -TERM 4182 and kill -15 4182 are the same.

The Safe Order

  1. Send SIGTERM: kill 4182.
  2. Wait 10 to 30 seconds. A program that writes files or closes connections needs that time.
  3. Check whether it is gone: ps -p 4182 shows no line for that PID when it is.
  4. If it is still there, look at its state in ps -o pid,stat,cmd -p 4182. A process in state D waits for the disk, and usually no signal ends it until the I/O finishes.
  5. Only then, as the last resort: kill -9 4182.

SIGKILL gives the program no chance to finish writes, remove temporary files, or release locks. Data that was in its memory is lost.

Kill by Name with pkill and killall

Both match by name, so they can hit more than you expect. Always run pgrep -a with the same pattern first and read the list:

pgrep -a php
pkill php
pgrep -a -u deploy
pkill -u deploy

pkill matches part of the process name; with -f it matches the whole command line, which can include unrelated processes. killall nginx matches the exact name. killall is part of the psmisc package and may need to be installed. Both send SIGTERM unless you name another signal.

Stop a Service with systemctl, Not kill

If the process belongs to a systemd service, stop the service:

sudo systemctl stop nginx
sudo systemctl status nginx

systemd stops all of the service's processes in order. A process you kill by hand may be started again by systemd, depending on the unit's Restart= setting, and the unit may be left in a failed state. See how to list services to find the unit name and systemd unit files for restart settings.

Never kill -9 a Database

MySQL, MariaDB, PostgreSQL, and similar servers keep data in memory and write it in a set order. A SIGKILL can leave tables that need recovery at the next start. Stop them with sudo systemctl stop mariadb (or mysql, postgresql), and give a slow shutdown time to finish.

Zombie Processes

A zombie is a process that has already exited but whose parent has not yet collected its exit status. It uses no CPU or memory, only a line in the process table, and kill cannot remove it because it is no longer running. List zombies with their parent PID:

ps -eo pid,ppid,stat,comm | awk '$3 ~ /^Z/'

A few zombies are harmless. If their number keeps growing, restart the parent program, or its service, with SIGTERM or systemctl restart; when the parent exits, the zombies are cleaned up.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations