While working on the Linux terminal, there are times when you may need to stop a command or process that is currently in execution. This may be due to a frozen command, or maybe you’ve initiated a process and realize that it’s going to take longer than expected. Whatever the reason, I’ll guide you on how to stop a running command in Linux.
The easiest and most well-known way to stop a Linux command is by using the keyboard combination Control + C. This sends a SIGINT signal which interrupts and stops the current process.
For example, if you’ve initiated a command such as ping www.google.com and you want to stop it, simply press Control + C.
Using Control+Z and fg/bg Commands
In some cases, you might want to stop a process temporarily and resume it later. This is where the Control + Z command comes in handy. This combination sends a SIGTSTP signal which pauses the process. You can then resume the process either in the foreground using the fg command or in the background using the bg command.
Using the kill Command
The kill command is another way to stop a process. It sends a SIGTERM signal which requests a process to stop. However, the process has the ability to ignore this signal. If the process cannot be stopped with a SIGTERM, a SIGKILL signal can be used which forcefully stops the process.
$ kill PID $ kill -9 PID
Replace PID with the process ID that you wish to stop.
We’ve covered a few methods on how to stop a Linux command. Start with Control + C, if that doesn’t work then try using the kill command. Remember, it’s always best to request a process to stop before forcefully stopping it, to prevent potential data loss. Happy Linux-ing!