Linuxopsys Profile picture
Mar 26 33 tweets 6 min read
Knowing when and how to stop running processes is a essential skill for sysadmins. When a process becomes stuck, it only takes a gentle nudge to restart or stop it.

At times, a process takes all the system resources. In both cases, you need a cmd that lets you manage a process.
The Linux operating system includes a number of commands for terminating errant processes (rogue processes), such as pkill, kill, and killall.

This thread will teach you how to use the kill command in Linux.
𝗚𝗲𝘁𝘁𝗶𝗻𝗴 𝘁𝗼 𝗸𝗻𝗼𝘄 𝗟𝗶𝗻𝘂𝘅 𝘀𝗶𝗴𝗻𝗮𝗹𝘀

Linux processes use signals to communicate with one another. A process signal is a predefined message that processes can either ignore or respond to. Developers define how a process will handle signals.
Below are some of the Linux signals that processes can receive and respond to:
𝗸𝗶𝗹𝗹 𝗰𝗼𝗺𝗺𝗮𝗻𝗱 𝗶𝗻 𝗟𝗶𝗻𝘂𝘅

Most shells, including Bash and zsh, include kill commands. The /bin/kill or /usr/bin/kill executable, as well as the shell's built-in kill, behave in slightly different ways.
To find out all the locations on your system hwere the kill command is located, use the type command and the -a option:

$ type -a kill
The output above shows that when you type kill on the command line, the shell builtin is used rather than the executable binaries. To use the executable binary, enter its full path (for example, /bin/kill or /usr/bin/kill).
In this thread , we will discuss the bash built-in command, so if you are using another shell, you must switch to bash to get similar results.
𝗸𝗶𝗹𝗹 𝗰𝗼𝗺𝗺𝗮𝗻𝗱 𝗶𝗻 𝘀𝘆𝗻𝘁𝗮𝘅

The kill command has the following syntax:

$ kill [options] <pid> [...]
You can use the kill command to send signals to processes based on their process IDs (PIDs). By default, when the kill command is used without specifying the signal, all of the PIDs specified on the command line receive a SIGTERM signal.
The following are the most commonly used signals:

- 1 (SIGHUP) - Hangs up the process.
- 9 (SIGKILL) - Unconditionally terminates the process.
- 15 (SIGTERM) - Stop a running process gracefully.
Use the kill command with the -l option to see a list of all the available signals that can be sent with the kill command:

$ kill -l
You must be the process owner or logged in as the root user to send a process signal. Ordinary users can only send signals to their own processes. To send signals to the process of other users you must be a root user.
You can use the -s option to specify which process signals to send by using their name or signal number. There are several ways to specify signals, as shown below:

- Using the equivalent signal number (-1 or -s 1).

-Prefixing the signal name with “SIG”(-SIGHUP or -s SIGHUP).
- Omitting the “SIG” prefix (-HUP or -s HUP).

So go ahead and select the option that works best for you.
𝗛𝗼𝘄 𝘁𝗼 𝗳𝗶𝗻𝗱 𝗟𝗶𝗻𝘂𝘅 𝗽𝗿𝗼𝗰𝗲𝘀𝘀𝗲𝘀 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻

When a program runs on the system, it is referred to as a process. To get a glimpse of these processes, you must first become acquainted with ps command.
This command can display a large amount of information about all of the programs currently running on your system.

Here is an example of ps basic syntax:

$ ps
As you can see, the default ps doesn't provide a lot of information. The cmd, by default, only displays processes that are currently running on the current terminal and are owned by the current user. In this case, only the bash shell, the zsh shell, and the ps cmd were running.
The basic output shows the process ID (PID) of the application, the terminal (TTY) from which it is being executed, and the amount of CPU time each process has consumed. To view more information about the programs, combine the ps with several options.
If you want to learn more about the various options available with ps, simply type 'man ps' into your terminal.

$ man ps
Despite being excellent for learning about the processes that are active on the system, ps has one drawback. This command displays only information for a specific time period.
The ps command makes identifying patterns in processes that switch in and out of memory difficult. Instead, the top command is useful.

The top command, like the ps command, displays process information in real time. Here's an example of a top showing real-time processes.
𝗸𝗶𝗹𝗹 𝗰𝗼𝗺𝗺𝗮𝗻𝗱 𝗼𝗽𝘁𝗶𝗼𝗻𝘀

Here is a list of the most common kill command otpions you should be aware of:
𝗛𝗼𝘄 𝘁𝗼 𝗸𝗶𝗹𝗹 𝗽𝗿𝗼𝗰𝗲𝘀𝘀𝗲𝘀 𝗶𝗻 𝗟𝗶𝗻𝘂𝘅

To terminate a process with the kill command, you must first obtain its process ID (PID). To accomplish this, various commands such as ps, pgrep, top (or other top variants such as htop, btop++, etc.), and pidof can be used
Now that you understand signals and how to locate processes on your system, let's use the kill command to terminate a process.
𝗞𝗶𝗹𝗹 𝗮 𝗽𝗿𝗼𝗰𝗲𝘀𝘀 𝘂𝘀𝗶𝗻𝗴 𝗽𝗿𝗼𝗰𝗲𝘀𝘀𝗲𝘀 𝗜𝗗

Assume you need to terminate  zsh process because it is no longer responding. Its PIDs can be found using the pgrep command. The pgrep command is used to determine the process id of a particular process

$ pgrep zsh
Notice the above command prints the process IDs of all the zsh processes:

You can now then kill all processes once you know their PIDs by sending a KILL signal (SIGKILL or -9) :

$ kill -9 6698 6792 6816 6846
Here the kill command sends the kill signals to all the processes that belong to the vscode program.
𝗞𝗶𝗹𝗹 𝗮 𝗽𝗿𝗼𝗰𝗲𝘀𝘀 𝘂𝘀𝗶𝗻𝗴 𝘁𝗵𝗲 𝗽𝗿𝗼𝗰𝗲𝘀𝘀 𝗻𝗮𝗺𝗲

The kill command has a significant disadvantage in that you cannot kill processes by name. The pkill command is an excellent tool for terminating processes by name rather than PID number.
However, there is a workaround for the kill command by combining the above command with the kill command.

$ kill -9 $(pgrep firefox)
You can also achieve the same thing with pidoff as shown:

$ kill -9 $(pidof firefox)

This way the kill command will send the specified signal to all vscode processes.
𝗦𝘂𝗺𝗺𝗶𝗻𝗴 𝘂𝗽!

In this tutorial, we learned about the kill command in Linux and how to use it. For more information visit the kill man page or simply type "man kill" from the terminal.
That's it for this thread:

If you found this thread valuable:

1. Toss us a follow for more daily threads on Linux, sysadmin, and DevOps → @linuxopsys

2. Like and RT the first tweet so other Linux folks can find it too.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Linuxopsys

Linuxopsys Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @linuxopsys

Mar 25
pidof is a Linux command-line utility that returns the process IDs of a specific running program by its name.

Those IDs are printed on the standard output. A process ID is a unique identifier assigned to each process when it is created on the system.

Learn more in this thread:
The basic sysntax of pidof is as follows:

$ pidof [OPTIONS] PROGRAM_NAME
The pidof command takes zero or more program names as parameters, but usually, only one program name is passed to it.
Read 13 tweets
Mar 25
pidof is a Linux command-line utility that returns the process IDs of a specific running program by its name.

Those IDs are printed on the standard output. A process ID is a unique identifier assigned to each process when it is created on the system.

Learn more in this thread:
The basic sysntax of pidof is as follows:

$ pidof [OPTIONS] PROGRAM_NAME
The pidof command takes zero or more program names as parameters, but usually, only one program name is passed to it.
Read 13 tweets
Mar 23
As a sysadmin or Linux power user, how do you transfer files from your local system to a remote?

To synchronize directories or files to another machine we need a fast and reliable tool. In this thread, we will learn about the rsync command in Linux using some practical examples.
rsync is a command-line utility that can synchronize files and directories between two locations via a remote shell or from/to a remote Rsync daemon.

It enables rapid incremental file transfer by transferring only the differences between the source and destination files.
Rsync can be used to mirror data, perform incremental backups, copy files between systems, and to replace the scp, sftp, and cp commands.
Read 46 tweets
Mar 20
20 Basic Linux commands that every new user should be aware of, with explanations :↓
Linux has a command for practically every operation, and the bulk of them are straightforward and quick to use.

In this thread, I've compiled a list of 18 helpful utilities for Linux users that you'll find useful if you use Linux on a daily basis.
Getting Help in Linux 🐧

• help - in Bash shell, without any parameters the command will display the list of all available built-in bash commands.

If you specify the command name as a parameter it will display the info about the bash command you specify.
Read 15 tweets
Mar 16
4 Stages of modern Linux boot process explained (bookmark this)🐧↓
When you turn on your Linux computer, it goes through a series of phases before presenting a login screen that prompts you for your username or password.

Every Linux distribution goes through four distinct stages during the boot-up process.
The booting process consists of four steps, which we will go over in this thread:

• BIOS and UEFI Integrity check (POST)
• Loading of the Boot loader (GRUB2)
• Kernel initialization
• Starting systemd, the parent of all processes
Read 40 tweets
Mar 9
How to compress and decompress files in Linux using gzip command🐧 ↓
Gzip is a popular compression algorithm that allows you to reduce the size of a file while retaining its original file mode, ownership, and timestamp.
Gzip also refers to the .gz file format as well as the gzip utility for compressing and decompressing files.

In this thread, you will learn how to use the gzip command to compress files in Linux.
Read 25 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(