Process Management – Linux Hint https://linuxhint.com Exploring and Master Linux Ecosystem Thu, 25 Feb 2021 03:24:38 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.2 Managing Processes In Ubuntu Linux https://linuxhint.com/manage-processes-ubuntu-linux/ Mon, 22 Feb 2021 09:21:27 +0000 https://linuxhint.com/?p=90810 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.

]]>
How to Set Max User Processes on Linux https://linuxhint.com/set_max_user_processes_linux/ Sun, 08 Nov 2020 05:07:51 +0000 https://linuxhint.com/?p=76037 Linux offers the capability to customize almost every single aspect of your system. One such feature is the ability to control the number of processes a user can have. This gives the system admins better control over the system and optimizes resource consumption. This article will show you how to set max user processes in Linux.

Setting Max User Processes

A single user has the capability to run a large number of processes. Linux is a multi-user operating system. Now, imagine multiple users running tons of processes. Even if each of the processes does not consume too many hardware resources on its own, the sum of all user processes may eventually hog the entire system. To avoid such a situation, system admins may limit the number of processes that each user can open.

The limit can be imposed temporarily or permanently. Depending on your target scenario, follow the most appropriate method.

Set Max User Processes Temporarily

This method temporarily changes the limit of the target user. If the user restarts the session or the system is rebooted, the limit will reset to the default value.

Ulimit is a built-in tool that is used for this task. Ulimit can impose limits on various resources for a particular user. The only downside (as mentioned earlier) is that it is temporary.

Log in as the target user and check the current process limit.

$ ulimit -u


Next, define the limit to 12345.

$ ulimit -u 12345


Verify the change.

$ ulimit -u

Set Max User Processes Permanently

The following method is more reliable in a real-life scenario. If the user logs out or the system reboots, the change will not disappear.

Here, we will still use the Ulimit tool. However, instead of directly making changes using the ulimit command, we will tweak the system configuration tool that Ulimit uses to assign the limits to the target users.

Check out the limits.conf. command below:

$ cat /etc/security/limits.conf


To add an entry to the file, it should be in the following format:

$ <domain> <type> <item> <value>

The following list provides a definition for each field:

  • domain: A domain can be a user, user group, GUID ranges, etc.
  • type: The type of the limit. It can be either hard or soft.
  • item: What resource will be limited. For this guide, we’ll be using “nproc”.
  • value: The value of the limit.

Next, we will discuss limit types. If you want the domain to have a maximum process limit hovering around a certain value, then you use the soft limit type. If you want the domain to have a maximum process limit at a fixed value, then you use the hard limit type.

As for the item fields, there are a number of these. For the full list, I recommend checking out the limits.conf man page.

$ man limits.conf


Now, back to tweaking the limits.conf file. To edit the file, open it in your favorite text editor. Note that you must run the command with root privileges. Otherwise, the changes cannot be saved.

$ sudo vim /etc/security/limits.conf


The following line is just an example. It will set a hard limit of 12345 for the user viktor.

$ viktor hard nproc 12345


Save the file and close the editor. To take the changes into effect, the user may need to restart the session or the system may have to reboot.

Verify the result.

$ ulimit -a viktor

Conclusion

Limiting max user processes is a common task that system admins may have to perform. Hopefully, this guide was helpful in that regard.

If you are interested in learning more about how to impose limits on other resources, check out the article Linux ulimit command. Ulimit is the primary tool that imposes resource limits on users in Linux.

Happy computing!

]]>
How to kill a process on Linux https://linuxhint.com/kill_process_linux-2/ Wed, 06 May 2020 13:34:46 +0000 https://linuxhint.com/?p=59659 There will likely be times when you encounter issues with unresponsive applications and processes. Sometimes closing and even restarting them does not work. In such cases, the only option that comes in mind is to restart the system which is time taking process and sometimes not acceptable in case of servers running a number of critical services.There are some other useful ways to get around this issue by terminating the process without needing to restart the system. This article will explain such ways which can be used to kill a process in a Linux OS.

Note: We have explained the procedure mentioned in this article on Ubuntu 20.04 LTS. More or less the same commands and procedures can be followed in previous versions of Ubuntu,

Using the System Monitor

The Gnome system monitor is a built-in GUI utility that can be used to kill a process in Linux OS. It allows to stop a process and then resume it with the Continue option. The end option allows terminating a process safely while the kill option forcefully terminates the program.

If System Monitor is not installed in your system, you can install it as follows:

$ sudo apt-get install gnome-system-monitor

To launch System Monitor, hit the super key and search it using the search bar at the top. When the search result appears as follows, hit Enter to open it.

In the System Monitor window, you will see a list of processes running in your system. In order to kill a process, right-click it, and select Kill option. It will kill the selected process instantly.

Kill process using the keyboard shortcuts in Terminal

Linux command line includes a number of useful keyboard shortcuts. Among them, following shortcuts can be used to kill a running process.

Ctrl+C: It sends SIGINT that terminates the running process

Ctrl+Z: It sends SIGSTP that suspends an application and send it to the background. However, it does not terminate the process. To view the stopped process, you can use the jobs command. Use fg command to bring the process to the foreground.

Ctrl+\: It sends SIGQUIT that terminates the process. It also creates a core dump file which can be used to debug the process.

Kill process using the xkill utility

Xkill allows killing a running program using the mouse cursor. It is GUI based utility that is pre-installed in most of the systems. If not already installed, you can install it as follows:

$ sudo apt install xorg-xkill

To close any program, simply type this in your command line Terminal:

$ xkill

Running the above command will turn your mouse cursor in to x shape. Now place the cursor on the program that you want to close and left-click on it.

Set shortcut for Xkill

You can set a shortcut for xkill that will allow you to immediately kill an application without the need of opening the Terminal and running the command.

To create a shortcut for xkill, open the Settings utility using the right-click menu from the desktop. Then open the Keyboard Shortcuts tab and click the + icon at the very bottom.

Then in the following dialog, name the shortcut and type xkill in the Command field and click Set Shortcut button.

Then set a custom shortcut of your choice and click the Add button.

Now whenever you need to kill an application, simply press the shortcut keys and you will be able to kill any open application in your system.

Kill process using the Kill commands

There are also some command-line ways used to kill the processes in Linux which includes kill, pkill, and killall.

To find which processes are currently running in your system, you can use the ps command with –A flag:

$ ps –A

It will list all the currently running processes in your system.

Kill

Kill command can be used to kill a running process in Linux. The kill command is provided with a PID of a process to be killed.

To find the process ID of a running process, you can use ps –A command. Alternatively, you can pipe the output of ps with grep command to find the process ID of a specific process:

$ ps –A | grep <processname>

For example:

To find the process ID of arunning Firefox program, you can use:

$ ps –A | grep firefox

Once you have found the PID of a specific process, you can kill it as follows:

$ kill PID

The kill command sends a SIGTERM signal to the specified PID which asks the process to terminate after performing the necessary cleanup operation.

In some scenarios, running the kill command does not terminate the process. If this is the case, you will need to type “kill -9” followed by the PID:

$ kill -9 PID

Using the -9 option with kill command sends a SIGKILL signal which asks the process to terminate immediately without any cleanup operation.

Pkill

Similar to kill command, pkill also sends a SIGTERM signal which allows terminating an unresponsive process. However, the good thing about pkill is that you do not have to provide the PID of a process in order to kill it. Instead, you can just provide the matching keyword related to the process.

$ pkill <keyword>

For example, to kill Firefox program, you can just type:

$ pkill firef

It will kill all the processes whose names match with the mnentioned <keyword>.

With pkill, you also have an option to kill the process running by a specific user:

$ pkill –u <username> < keyword>

Be careful when using this option as If you do not specify the <keyword>, all the processes with the specified username will be killed.

Killall

Killall command is similar to pkill except it takes the full process name as an argument instead of any matching keyword.

In order to use killall to terminate all the processes and their child processes with a specific name, use the following syntax:

$ killall <prcoessname>

For example:

$ killall firefox

That is all there is to it! In this article, you have learned various ways to kill a process in Linux. Use these commands with care as killing a process causes it to end immediately resulting in a data loss. Also killing the wrong process might end up disturbing the system.

]]>
How to Kill a Process in Linux https://linuxhint.com/kill_process_linux/ Wed, 12 Feb 2020 18:46:40 +0000 https://linuxhint.com/?p=55139 Every Linux operating system comes with the kill command. The sole purpose of this tool is to terminate a target process. It’s a powerful tool that makes Linux quite versatile, especially in server and enterprise fields where a major change/update can take effect without restarting the entire machine. In this article, I’ll be showcasing how to kill a process using kill, pkill and killall.

Killing a process

For killing a process, we’ll be using a handful of tools: kill, pkill, and killall. All of them work basically the same way.

These tools don’t terminate the process themselves. Instead, they send a designated signal to the target process or process groups. If you didn’t specify any specific signal, SIGTERM is sent as the default signal. However, there are a number of supported signals, for example, SIGKILL, SIGHUP etc.

Here’s the basic command structure for kill, pkill and killall.

$ kill <signal_or_options> <PID(s)>
$ pkill <signal_or_options> <process_name>
$ killall <option> <process_name>

Whenever possible, it’s recommended to use kill.

Kill, pkill and killall locations

Kill is the default command for terminating a process.

$ kill --help

It’s run from /usr/bin directory.

$ which kill

The benefit is, it also allows access to pkill, another command similar to kill that allows terminating process based on their name.

$ pkill --help

$ which pkill

Some apps run multiple processes of the same executable. If you want to terminate a number of processes with the same name, use the killall tool.

$ killall --help

$ which killall

Listing all the running processes

The very first task is identifying the PID (process identification number) and/or the process name that you’d like to terminate. For this example, I’ll be using Firefox as the target process to terminate. Run the following command to list all the running processes on the system.

$ ps -A

For most of the tasks, we need to know the PID of the target process. However, in certain situations, using the process name is more suitable.

If you know the exact name of the target process, you can directly get the PID using pidof.

$ pidof <process_name>

Another interesting tool to grab the information about the target process is pgrep. It’s specifically designed for the purpose.

$ pgrep <option> <process_name>

Kill signals

Now, let’s have a look at the signals that the kill tools support. It’s a huge list. Of course, not all of them are necessary for every single situation. In fact, most of the situations require only a handful of signals.

First, let’s take a look at the list that kill supports.

$ kill -l

There are 2 ways to define which signal you want to send. You can either use the full signal name or its equivalent value.

$ kill -<signal> <PID>

Or,

$ kill -<signal_value> <PID>

The most popular signals are SIGHUP (1), SIGKILL (9) and SIGTERM (15).  Generally, SIGTERM is the default and safest way to terminate a target process.

In the case of pkill, the supported signal is the same as kill. However, in the case of killall, the number of supported signals and the signal names are different.

$ killall -l

Killing a process

To kill a process, we need the PID of that target process. Assuming that you have the PID, run the following command to kill it.

$ kill <option> <PID>

Here, kill will send the default signal SIGTERM to the PID(s). If you wanted to terminate multiple processes, then mention all the PIDs separated by space.

$ kill <option> <PID_1> <PID_2>

Let’s specify which signal you’d like to send to the target.

Want to terminate a process using its name only? Use pkill.

$ pkill <option> <process_name>

In some cases, a particular application may have too many processes running. Typing all those PIDs is time-consuming and tiresome. In such scenarios, we’ll be using the killall tool. It’s quite similar to kill but it works with process name.

$ killall <option> <process_name>

For example, when running Firefox, it starts a handful of processes. To kill all of them at once, run this command.

$ killall firefox

Want to terminate all the processes that are running under a certain user? Killall can do the job, no problem. Be careful when running this one as it might break down the system and create other issues. It won’t work if you’re trying to terminate processes that are running under a different user with higher privilege.

$ killall -u <user>

Permission conflict

The inherent characteristics of the Linux user hierarchy also apply when you’re about to terminate an application. A user can’t terminate processes that are running with higher privilege, only a processes with equal/lower privilege. Moreover, a user can’t manipulate processes that are running under different user.

For example, let’s consider the yes command. If it’s called as the current user, it can be easily terminated using kill.

$ kill yes

Now, what if yes was running under root? Calling kill as the current user won’t work.

Similarly, if a process was running under another user, you can’t terminate it from a different user account.

Final thoughts

In this article, only the basics and common usage of these commands were showcased. These kill tools are capable of more than that. To have in-depth knowledge of the abilities of any tool, I recommend checking out the man page.

$ man kill

$ man pkill

Enjoy!

]]>