Ubuntu

Managing Processes In Ubuntu Linux

Managing processes in Linux is an important topic to learn and understand, as it is a multitasking operating system and has many processes ongoing at the same time. Linux provides many tools for managing processes, like listing running processes, killing processes, monitoring system usage, etc. In Linux, every process is represented by its Process ID (PID). There are some other attributes to the process like user id and group id if a user or group runs the process. Sometimes you need to kill or interact with a process, so you should know how to manage these processes to make your system run smoothly. In Linux, processes can be managed with commands like ps, pstree, pgrep, pkill, lsof, top, nice, renice and kill, etc.

Processes

Running an instance of a program is called a process. In Linux, process id (PID) is used to represent a process that is distinctive for every process. There are two types of processes,

  • Background processes
  • Foreground processes

Background Processes

Background processes start in the terminal and run by themselves. If you run a process in the terminal, its output will be displayed in a terminal window, and you can interact with it, but if you don’t need to interact with the process, you can run it in the background. If you want to run a process in the background, just add a “&” sign at the end of the command, and it will start running in the background; it will save you time, and you will be able to start another process. For listing processes running in the background, use the command ‘jobs.’ It will display all the running processes in the background.

For example, upgrading is a long process in Linux. It takes too much time, and if you want to do other stuff while the system is upgrading, use the background command.

ubuntu@ubuntu:~$ sudo apt-get upgrade -y &

It will start running in the background. And you can interact with other programs in the meanwhile. You can check how many and which processes are running in the background by typing this command.

ubuntu@ubuntu:~$ jobs
[1]+ Running sudo apt-get upgrade -y &

Foreground processes

All the processes that we run in the terminal are, by default, run as foreground processes. We can manage them by foreground and background commands.

You can bring any background process listed in jobs to the foreground by typing the ‘fg’ command followed by the background process number.

ubuntu@ubuntu:~$ fg %1
sudo apt-get upgrade -y

And if you want to take this process to the background type this command.

ubuntu@ubuntu:~$ bg %1

Listing and managing processes with ps command

The listing process with the ps command is one of the oldest ways to view the terminal running processes. Type ps command to list which processes are running and how much system resource they are using and who is running them.

ubuntu@ubuntu:~$ ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
Jim 1562 0.0 0.0 164356 6476 tty2 Ssl+ 13:07 0:00 shell
Jim 1564 5.2 0.9 881840 78704 tty2 Sl+ 3:07 13:13 dauth
Jim 2919 0.0 0.0 11328 4660 pts/0 Ss 13:08 0:00 bash
Jim 15604 0.0 0.0 11836 3412 pts/0 R+ 17:19 0:00 ps u
...snip...

The user column shows the user name in the above table, and PID shows the process id. You can use the PID to kill or send the kill signal to a process. %CPU shows CPU percentage processor, and %MEM shows random access memory usage. To kill a process, type.

ubuntu@ubuntu:~$ kill [ process id (PID) ]

or

ubuntu@ubuntu:~$ kill -9 [ process id (PID) ]

Use the ps aux command to see all running processes and add a pipe to see it in order.

ubuntu@ubuntu:~$ ps aux | less

If you want to rearrange the columns, you can do it by adding a flag -e for listing all the processes and -o for indicating for the columns by keywords in the ps command.

ubuntu@ubuntu:~$ps -eo pid,user,uid,%cpu,%mem,vsz,rss,comm
PID USER UID %CPU %MEM VSZ RSS COMMAND
1 root 0 0.1 0.1 167848 11684 systemed
3032 jim 1000 16.5 4.7 21744776 386524 chrome
...snip...

Options for ps command.

u option is used for listing the processes by the users.

ubuntu@ubuntu:~$ ps u

f option is used to display the full listing.

ubuntu@ubuntu:~$ ps f

x option is used to display information about the process without a terminal.

ubuntu@ubuntu:~$ ps x

e option is used to display extended information.

ubuntu@ubuntu:~$ ps e

a option is used for listing all the processes with the terminal.

ubuntu@ubuntu:~$ ps a

v option is used to display virtual memory format.

ubuntu@ubuntu:~$ ps v

Flags for ps command.

-e flag is used to see every process on the system.

ubuntu@ubuntu:~$ ps -e

-u flag is used to see processes running as root.

ubuntu@ubuntu:~$ ps -u

-f flag is used for a full listing of processes.

ubuntu@ubuntu:~$ ps -f

-o flag is used for listing the processes in the desired column.

ubuntu@ubuntu:~$ ps -o
pstree

pstree is another command to list the processes; it shows the output in a tree format.

ubuntu@ubuntu:~$ pstree

Options for pstree command

-n is used for sorting processes by PID.

ubuntu@ubuntu:~$ pstree -n

-H is used for highlighting processes.

ubuntu@ubuntu:~$ pstree -H [PID]
ubuntu@ubuntu:~$ pstree -H 6457

-a is used for showing output, including command-line arguments.

ubuntu@ubuntu:~$ pstree -a

-g is used for showing processes by group id.

ubuntu@ubuntu:~$ pstree -g

-s is used for sowing the tree or specific process.

ubuntu@ubuntu:~$ pstree -s [PID]
ubuntu@ubuntu:~$ pstree -s 6457

[userName] is used for showing processes owned by a user.

ubuntu@ubuntu:~$ pstree [userName]
ubuntu@ubuntu:~$ pstree jim
pgrep

With the pgrep command, you can find a running process based on certain criteria. You can use the full name or abbreviation of the process to find or by username or other attributes. pgrep command follows the following pattern.

ubuntu@ubuntu:~$ Pgrep [option] [pattern]
ubuntu@ubuntu:~$ pgrep -u jim chrome
Options for pgrep command

-i is used for searching case insensitive

ubuntu@ubuntu:~$ Pgrep -i firefox

-d is used for delimiting the output

ubuntu@ubuntu:~$ Pgrep -u jim -d:

-u is used for finding process owned by a user

ubuntu@ubuntu:~$ Pgrep -u jim

-a is used for listing processes alongside their commands

ubuntu@ubuntu:~$ Pgrep -u jim -a

-c is used for showing the count of matching processes

ubuntu@ubuntu:~$ Pgrep -c -u jim

-l is used for listing processes and their name

ubuntu@ubuntu:~$ Pgrep -u jim -l
pkill

With the pkill command, you can send a signal to a running process based on certain criteria. You can use the full name or abbreviation of the process to find or by username or other attributes. pgrep command follows the following pattern.

ubuntu@ubuntu:~$ Pkill [Options] [Patterns]
ubuntu@ubuntu:~$ Pkill -9 chrome
Options for pkill command

–signal is used for sending a signal e.g. SIGKILL, SIGTERM, etc.

ubuntu@ubuntu:~$ Pkill --signal SIGTERM vscode

-HUP is used for reloading a process

ubuntu@ubuntu:~$ Pkill -HUP syslogd

-f is used for killing processes based on the full command-line.

ubuntu@ubuntu:~$ Pkill -fping 7.7.7.7”

-u is used for killing all the processes owned by a user.

ubuntu@ubuntu:~$ Pkill -u jim

-i is used for case insensitive killing of the process by pkill.

ubuntu@ubuntu:~$ Pkill -i firefox

-9 is used for sending a kill signal.

ubuntu@ubuntu:~$ Pkill -9 chrome

-15 is used for sending a termination signal.

ubuntu@ubuntu:~$ Pkill -15 vlc
lsof (List of Open Files)

This command-line utility is used for listing files opened by several processes. And as we know, all UNIX/Linux systems recognize everything as a file, so it is convenient to use the lsof command to list all opened files.

ubuntu@ubuntu:~$ lsof

In the above table of lsof command, FD represents file description, cwd represents the current working directory, txt means text file, mem means memory-mapped files, mmap means memory-mapped devices, REG represents a regular file, DIR represents Directory, rtd means root directory. There are other options you can use with the lsof command.

Options for lsof command.

-c is used for the listing of open files by their process name.

ubuntu@ubuntu:~$ lsof -c chrome

-u is used for the listing of open files by a user.

ubuntu@ubuntu:~$ lsof -u jim

-i is used for the listing of processes executing on a port.

ubuntu@ubuntu:~$ lsof -i

+D is used for the listing of open files under a directory.

ubuntu@ubuntu:~$ lsof +D /home/

-p is used for the listing of open files by a process.

ubuntu@ubuntu:~$ lsof -p 1342

Listing and Managing Process With top Command

With the top command, you can display a real-time view of system processes running. It displays the processes depending upon CPU usage. You can sort the column according to you. The top command also provides some information about your system, like how long the system has been running up or how many users are attached to the system and how many processes are running, how much CPU and RAM is being used, and a listing of each process.

Type the command top to list down the processes running.

ubuntu@ubuntu:~$ top

Tasks: 291 total, 1 running, 290 sleeping, 0 stopped, 0 zombie

%Cpu(s) : 2.3us, 0.3sy, 0.0ni, 97.0id, 0.3wa, 0.0hi, 0.0si, 0.0st

MiB Mem: 7880.6 total, 1259.9 free, 3176 used, 3444.4 buff/cache

MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 4091.8 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

3241 jim 20 0 20.7g 33512 10082 S 1.7 4.2 0:54.24 chrome

3327 jim 20 0 4698084 249156 86456 S 1.3 3.1 1:42.64 chrome

2920 jim 20 0 955400 410868 14372 S 1.0 5.1 7:51.04 chrome

3423 jim 20 0 4721584 198500 10106 S 1.0 2.5 0:49.00 chrome

3030 jim 20 0 458740 114044 66248 S 0.7 1.4 3:00.47 chrome

3937 jim 20 0 4610540 104908 72292 S 0.7 1.3 0:05.91 chrome

1603 jim 20 0 825608 67532 40416 S 0.3 0.8 3:13.52 Xorg

1756 jim 20 0 4154828 257056 10060 S 0.3 3.2 5:53.31 gnome-s+

1898 jim 20 0 289096 29284 5668 S 0.3 0.4 1:06.28 fusuma

3027 jim 20 0 587580 14304 75960 S 0.3 1.8 9:43.59 chrome

3388 jim 20 0 4674192 156208 85032 S 0.3 1.9 0:13.91 chrome

3409 jim 20 0 4642180 140020 87304 S 0.3 1.7 0:15.36 chrome

3441 jim 20 0 16.5g 156396 89700 S 0.3 1.9 0:25.70 chrome

….snip….

You can also do some actions with the top command to make changes in running processes; here is the list below.

  • u by pressing “u” you can display a process running by a certain user.
  • M by pressing “M” you can arrange by RAM usage rather than CPU usage.
  • P by pressing “P” you can sort by CPU usage.
  • 1 by pressing “1” switch between CPUs usage if there are more than one.
  • R by pressing “R” you can make your output sort reverse.
  • h by pressing “h” you can go to help and press any key to return.

Note which process is consuming more memory or CPU. Those processes which are consuming more memory can be killed, and those processes which are consuming more CPU can be reniced to give them less importance to the processor.

Kill a process at the top: Press k and write the Process ID you want to kill. Then type 15 or 9 to kill normally or immediately; you can also kill a process with a kill or killall command.

Renice a process at the top: Press r and write the PID of the process you want to be reniced. It will ask you to type the PID of the process and then the value of nicing you want to give this process between -19 to 20 (-19 means the highest importance and 20 means lowest importance).

Listing & Managing Processes With System Monitor

Linux has a system monitor gnome to show the running processes more dynamically. To start the system monitor, press the windows key and type the system monitor, click on its icon, and you can see processes in columns. By right-clicking them, you can kill, stop, or renice the process.

The running processes are displayed with user accounts in alphabetical order. You can sort the processes by any field headings like CPU, Memory, etc., just click on them, and they will be sorted; for example, click on CPU to see which process is consuming the most CPU power. To manage processes, right-click on them and select the option you want to do with the process. To manage the process select the following options.

  • Properties- show other settings related to a process.
  • Memory Maps- show system memory maps to show which library and other components are being used in memory for the process.
  • Open file- shows which files are opened by the process.
  • Change Priority- display a sidebar from which you can renice the process with the options from very high to very low and custom.
  • Stop- pauses the process until you select to continue.
  • Continue- restarts a paused process.
  • Kill- Force kills a process instantly.

Killing a process with kill and killall

kill, and killall command is used for Killing/ending a running process. These commands can also be used for sending a valid signal to a running process, like telling a process to continue, end, or reread configuration files, etc. Signals can be written in both ways by numbers or by name. The following are some commonly used signals.

Signal Number Description

SIGHUP 1 Detects hang-up signal on controlling terminal.

SIGINT 2 Interpreted from keyboard.

SIGQUIT 3 Quit from the keyboard.

SIGILL 4 Illegal instructions.

SIGTRAP 5 Is used for tracing a trape.

SIGABRT 6 is used for aborting signal from abort(3).

SIGKILL 9 Is used for sending a kill signal.

SIGTERM 15 Is used for sending a termination signal.

SIGCONT 19,18,25 Is used to continue a process if stopped.

SIGSTOP 17,19,23 Is used for stopping processes.

Different values of SIGCONT and SIGSTOP are used in different Unix/Linux operating systems. For detailed information about signals type man 7 signal terminal.

Using kill Command For Sending Signal To Process By PID.

Note the process to which you want to send a kill signal. You can find the process id (PID) by ps or top command.

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

7780 jim 20 0 12596 4364 3460 R 33.3 3.2 13:54:12 top

The top process is consuming 33.3% of the CPU. If you want to kill this process to save CPU usage, here are some ways to end this process with the kill command.

ubuntu@ubuntu:~$ kill 7780

ubuntu@ubuntu:~$ kill -15 7780 or $ kill -SIGTERM 7780

ubuntu@ubuntu:~$ kill -9 7780 or $ kill -SIGKILL 7780

Using killall Command To Send Signals To A Process By Name.

With the killall command, you don’t have to search for process id; you can send a kill signal to a process by name rather than process id. It can also kill more processes than you want if you are not careful, e.g., “killall chrome” will kill all chrome processes, including those you don’t want to kill. Sometimes it is useful to kill processes of the same name.

Like the kill command, you can type the signals by name or by number in the killall command. Kill any running process with the killall command; you only have to type its name and the signal you want to send. e.g., send a kill signal process firefox using the killall command, write the below command.

ubuntu@ubuntu:~$ killall -9 firefox

or

ubuntu@ubuntu:~$ killall SIGKILL chrome

Changing the process priority with nice and renice

Every process on your Linux system has an excellent value, and it is between -19 to 20. It decided which process would get more CPU access in the system. The lower the value of the nice, the more access a process has to the CPU process. Like -16 nice values have more access to the CPU than 18 nice values. Only a user with root privileges can assign a negative value of nice. A normal user can only assign the value of “nice” between 0 to 19. A regular user can only assign higher nice values and on its own processes. A root user can set any nice value to any process.

If you want to give a process more accessible to the CPU usage by assigning the nice value, type the following command.

ubuntu@ubuntu:~$ nice +3 chrome

And renice the process

ubuntu@ubuntu:~$ renice -n -6 3612

Conclusion

Here is the guide to managing your Linux system with ps, top, lsof, pstree, pkilll, kill, killall, nice, renice, etc. Some processes consume most of the CPU usage and RAM; knowing how to manage them increases your system speed and performance and gives you a better environment to run any processes you want more efficiently.

About the author

Usama Azad

A security enthusiast who loves Terminal and Open Source. My area of expertise is Python, Linux (Debian), Bash, Penetration testing, and Firewalls. I’m born and raised in Wazirabad, Pakistan and currently doing Undergraduation from National University of Science and Technology (NUST). On Twitter i go by @UsamaAzad14