Skip to main content

Terminal Basics

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:

Interrupt a running process with ^C (Ctrl+C) in the terminal
Interrupt a running process with ^C (Ctrl+C)

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:

Stopping system update with Ctrl+C (SIGINT)
Stopping system update with Ctrl+C (SIGINT)

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.

top command running in Ubuntu
Note the PID of the program you want to stop and press k

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.

Press k key and enter the PID of the program you want to stop

That's it. Enjoy learning Ubuntu with Learn Ubuntu :)