Stop a Running Command
Learn how to stop a running command in Ubuntu. Also learn to stop commands and processes running in the background.
Running a command/script/program might not always result in the expected outcome, but things go wrong sometimes. That's when a command needs to be stopped.
To stop a running command, use the keyboard shortcut Ctrl+C
. This shortcut implies "Interrupt" (the running process) in the terminal, not copy :)
Here's a quick demonstration:
I interrupted a ping command from executing further using the Ctrl+C shortcut. It induces a termination signal on the currently running process, which interrupts the process from continuing further.
Care to learn more? Let's dive into the topic.
Stopping a running program in the Terminal
To stop a running command in the terminal that's running in the foreground (actively executed in that Terminal instance), use Ctrl+C (for SIGINT), or Ctrl+Z (for SIGTSTP).
In the example mentioned above, while pressing Ctrl+C, a termination signal SIGINT
(Signal Interrupt) is given to the ping command which was running in the foreground, so it stops right away.
Here's another example to check out: I stop the updating of repositories with Ctrl+C:
For foreground processes that output stuff in the terminal, this method works fine.
Stopping commands running in the background
The above trick works with the command that is continuously running in the terminal. What about commands (processes) that are running in the background?
For that, you can use the kill command and provide it the PID (process ID) of the process you want to kill:
sudo kill -9 PID
The question is how to get the process ID? There are multiple ways to get that.
You can use the ps command:
ps aux
It will give you a huge list of process running in the system and their PID. You can identify the program you want to stop and get its PID.
Use top command to terminate background process
Alternatively, you can also use the top command:
top
It's a task manager and shows the running commands in an interactive manner.
Note the PID of the program you want to stop and press k
key. It will ask you to provide the PID of the program you want to kill or it will suggest the first program from the list.
That's it. Enjoy learning Ubuntu with Learn Ubuntu :)