Linux Processes
My Favorites: ps, pgrep, pkill
How to Manage Processes from the Linux Terminal: 10 Commands You Need to Know
The Linux terminal has a number of useful commands that can display running processes, kill them, and change their priority level. This post lists the classic, traditional commands, as well as some more useful, modern ones.
Many of the commands here perform a single function and can be combined — that’s the Unix philosophy of designing programs. Other programs, like htop, provide a friendly interface on top of the commands.
top
The top command is the traditional way to view your system’s resource usage and see the processes that are taking up the most system resources. Top displays a list of processes, with the ones using the most CPU at the top.
To exit top or htop, use the Ctrl-C keyboard shortcut. This keyboard shortcut usually kills the currently running process in the terminal.
htop
The htop command is an improved top. It’s not installed by default on most Linux distributions — here’s the command you’ll need to install it on Ubuntu:
sudo apt-get install htop
htop displays the same information with an easier-to-understand layout. It also lets you select processes with the arrow keys and perform actions, such as killing them or changing their priority, with the F keys.
We’ve covered htop in more detail in the past.
ps
The ps command lists running processes. The following command lists all processes running on your system:
ps -A
This may be too many processes to read at one time, so you can pipe the output through the less command to scroll through them at your own pace:
ps -A | less
Press q to exit when you’re done.
You could also pipe the output through grep to search for a specific process without using any other commands. The following command would search for the Firefox process:
ps -A | grep firefox
pstree
The pstree command is another way of visualizing processes. It displays them in tree format. So, for example, your X server and graphical environment would appear under the display manager that spawned them.
kill
The kill command can kill a process, given its process ID. You can get this information from the ps -A, top or pgrep commands.
kill PID
Technically speaking, the kill command can send any signal to a process. You can use kill -KILL or kill -9 instead to kill a stubborn process.
pgrep
Given a search term, pgrep returns the process IDs that match it. For example, you could use the following command to find Firefox’s PID:
pgrep firefox
You can also combine this command with kill to kill a specific process. Using pkill or killall is simpler, though.
pkill & killall
The pkill and killall commands can kill a process, given its name. Use either command to kill Firefox:
pkill firefox
killall firefox
We’ve covered pkill in more depth in the past.
renice
The renice command changes the nice value of an already running process. The nice value determines what priority the process runs with. A value of -19 is very high priority, while a value of 19 is very low priority. A value of 0 is the default priority.
The renice command requires a process’s PID. The following command makes a process run with very low priority:
renice 19 PID
You can use the pgrep trick above with renice, too.
If you’re making a process run at a higher priority, you’ll require root permissions. On Ubuntu, use sudo for that:
sudo renice -19 #
xkill
The xkill command is a way of easily killing graphical programs. Run it and your cursor will turn into an x sign. Click a program’s window to kill that program. If you don’t want to kill a program, you can back out of xkill by right-clicking instead.
You don’t have to run this command from a terminal — you can also press Alt-F2, type xkill and press Enter to use it from a graphical desktop.
We’ve covered binding xkill to a hotkey to easily kill processes.
The ps command
Type the following ps command to display all running process:
# ps -aux | less
OR
# ps aux | less
Where,
- A : Select all processes
- u : Select all processes on a terminal, including those of other users
- x : Select processes without controlling ttys
Task: see every process on the system
# ps -A
# ps -e
Task: See every process except those running as root
# ps -U root -u root -N
Task: See process run by user vivek
# ps -u vivek
Task: top command
The top program provides a dynamic real-time view of a running system. Type the top at command prompt:
# top
Output:
To quit press q, for help press h.
Task: display a tree of processes
pstree shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified, all process trees rooted at processes owned by that user are shown.
$ pstree
Sample outputs:
Task: Print a process tree using ps
# ps -ejH
# ps axjf
Task: Get info about threads
Type the following command:
# ps -eLf
# ps axms
SOURCE: https://www.cyberciti.biz/faq/show-all-running-processes-in-linux/
List A Process
# ps -eLf | grep THEPROCESSNAME
What is a PID?
A Linux or Unix process is running instance of a program. For example, Firefox is a running process if you are browsing the Internet. Each time you start Firefox browser, the system is automatically assigned a unique process identification number (PID). A PID is automatically assigned to each process when it is created on the system. To find out PID of firefox or httpd process use the following command:
pidof httpd pidof apache2 pidof firefox |
OR use the combination of ps command and grep command:
ps aux | grep httpd ps aux | grep apache2 ps aux | grep firefox |
Sample outputs:
kill command syntax
The syntax is:
kill [signal] PID kill -15 PID kill -9 PID kill -SIGTERM PID kill [options] -SIGTERM PID |
What Linux or Unix permissions do I need to kill a process?
Rules are simple:
- You can kill all your own process.
- Only root user can kill system level process.
- Only root user can kill process started by other users.
kill command examples
In this example, I am going to kill lighttpd server.
Step #1: Find out the PID (process id)
Use the ps or pidof command to find out PID for any program. For example, if process name is lighttpd, you can use any one of the following command to obtain process ID:
pidof lighttpd |
Sample outputs:
3486
OR
ps aux | grep lighttpd |
Sample outputs:
lighttpd 3486 0.0 0.1 4248 1432 ? S Jul31 0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf lighttpd 3492 0.0 0.5 13752 3936 ? Ss Jul31 0:00 /usr/bin/php5-cg
Step #2: kill the process using a PID
The PID # 3486 is assigned to the lighttpd process. To kill the lighttpd server, you need to pass a PID as follows:
# kill 3486
OR
$ sudo kill 3486
This will terminate a process with a PID of 3486.
How do I verify that the process is gone / killed?
Use the ps or pidof command:
$ ps aux | grep lighttpd
$ pidof lighttpd
A note about sending stronger signal # 9 (SIGKILL)
If no signal specified in the kill command, signal # 15 (SIGTERM), is sent by default. So the kill 3486 command is same as the following command:
# kill -15 3486
# kill -SIGTERM 3486
OR
$ sudo kill -15 3486
$ sudo kill -SIGTERM 3486
Sometime signal # 15 is not sufficient. For example, lighttpd may not be killed by signal #15 due to open sockets. In that case process (PID) # 3486 would be killed with the powerful signal # 9:
# kill -9 3486
# kill -SIGKILL 3486
OR
$ sudo kill -9 3486
$ sudo kill -SIGKILL 3486
Where,
- -9 or -SIGKILL – A special kill signal that nearly guarantee to kill the process with the iron fist.
How can I kill two or more PIDs?
The syntax is as follows to kill two or more PIDs as required can be used in a single command:
kill pid1 pid2 pid3 kill -15 pid1 pid2 pid3 kill -9 pid1 pid2 pid3 kill -9 3546 5557 4242 |
Say hello to killall command
This is a Linux only command. to kill processes by name. So no need to find the PIDs using the ‘pidof process’ or ‘ps aux | grep process’ command. Do not use killall command on Unix operating systems. This is a Linux specific command.
The syntax is
killall Process-Name-Here |
To kill the lighttpd server, enter:
# killall -15 lighttpd
OR
# killall -9 lighttpd
To kill the Firefox web-browser process, enter:
# killall -9 firefox-bin
As I said earlier, the killall command on UNIX-like system does something else. It kills all process and not just specific process. Do not use killall on UNIX system.
SOURCE: https://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/
Kill by Process Name:
pkill THEPROCESSNAME