A frozen program running on a Raspberry Pi is likely consuming memory, power, CPU time, screen space, and is likely getting in your way. If a program is frozen it’s serving no purpose except to waste your resources. In this guide I’ve looked at a variety of different ways to stop a program running on a Raspberry Pi depending on how the program is running (in a terminal, as a script such as Python, and graphically).
The easiest way to stop a program running on a Raspberry Pi is to open a terminal and use the kill command with the program’s process ID. Finding the process ID can be accomplished using the ps aux command. Alternatively, a Raspberry Pi program can be killed using the Task Manager, which is found under Accessories in the Raspberry Pi menu.
In short, to stop a program running:
- If you can’t see the program you need to terminate, use the process ID method
- If the program is running inside another program (e.g. a Python script running inside Python), try using CTRL + C
- If you’re not sure what the program is, use a task manager
This guide gives more detail on each of these methods below.
1. Kill the program using its process ID
Every program running on a Raspberry Pi has an ID number associated with it, a Process ID (PID). Once you find out this PID it can be used to terminate the process using the kill
command in the Raspberry Pi terminal.
To stop a program running on a Raspberry Pi, follow the steps below:
Step 1: Open a new terminal window
Open up a new terminal window on your Raspberry Pi.
Even if your terminal is stuck running a program, you will be able to open a second window that we are going to use to fix the first one. (This happened to me before – I froze my terminal by running a program that never terminated, and then had to figure out how to open another terminal to fix it; thankfully Raspberry Pi’s graphical interface makes it easy to open a second one)
Step 2: Find the Process ID
We need to use this new terminal window to find the Process ID.
Type the following command in the terminal to give you a list of all the process running on your Raspberry Pi: ps aux
Look for the name of the program you want to terminate on the right hand side. If it’s a Python program you want to terminate, the program might be called python instead of the name of the program. Check out the picture below where I’m using the terminal to find a program I wrote which is stuck in an infinite loop.
Now look for the corresponding number in the second column from the left. This number is the Process ID (or pid). This is the number we are going to give to the kill
command to stop the program running.
Step 3: Use the kill command to stop the program running
Now that we know the Process ID (pid), we can feed it to the kill
command and to stop the program running.
In the terminal window, type the following command (substitute the pid number from the previous step): sudo kill -9 pid
If you are asked for a password, type in the password for your Raspberry Pi “super user” account (by default this is raspberry).
This command is telling the Raspberry Pi OS to send a “level 9” signal to a process; this signal will force the program to terminate immediately. More details on the different signal levels can be found here.
2. Kill the program based on its name
A program running on Raspberry Pi can be stopped if you know the name of the name of the underlying process for the program. Often the name of the process is the same as the name of the program, but commonly on Raspberry Pi this is not the case; if you’re running a program inside Python for example, the process name will by python rather than the name of the script/program you’re running.
If you know the name of the program that’s running, you can use the terminal to stop the program from running. The steps to do this are:
- Open up a new Terminal
- Type in this command:
sudo ps aux | grep -i name_of_program
- Find the Process ID (this is the first number of the output)
- Type this command:
sudo kill -9 process_ID
This is the same process as using the kill command alone, except with a different way of finding the Process ID.
3. Use CTRL C
Sending “CTRL C” to a program is similar to using the kill command, though with a lower priority (I used level 9 above, and CTRL C is level 2). This asks the program nicely if it can exit, and most of the time a program will comply.
To stop running a Python script use CTRL C by selecting the window that the Python script is running in and press CTRL and C on the keyboard. This will tell Python to stop executing the script, even if it is stuck in an infinite loop or waiting for input. Pressing CTRL C will work if you are connected to your Raspberry Pi remotely using ssh or vnc.
CTRL C works on other apps as well, though usually it’s best on terminal apps that are stuck in an infinite loop or waiting for input. I’ve never had a problem using CTRL C when remotely connecting to my Raspberry Pi (so it can be used even if you don’t have a keyboard plugged in.
4. Use the task manager to send a kill signal
Raspberry Pi OS includes a graphical task manager that can be used to a kill command to programs running on the Pi. It works similarly to its Windows counterpart, including a graphical window listing all the programs running on the Raspberry Pi, and the amount of CPU and memory usage each program is consuming.
Using the task manager is best for graphical programs that are frozen or stuck, such as a web browser. I’ve found it to be quicker than opening up a terminal and finding the process ID. In the example below though I use the task manager to terminate my infinite hello program.
Follow the steps below to use the task manager to end a program running on Raspberry Pi:
Step 1: Open the task manager under Accessories
Click on the Raspberry Pi icon and click on the Accessories menu. Then click on Task Manager.
Step 2: Find the program that is running
A list of programs running on your Raspberry Pi will be presented in the task manager. Find the one that you want to stop running.
Step 3: Right click on the program and select kill
Selecting the option to kill the program is similar to using the kill
command from the terminal. Except this way you can use a mouse to do it and you don’t have to worry about finding the process ID.
5. Find the process ID using the command line task manager
Raspberry Pi OS also includes a task manager that can be accessed from the terminal command line. The command line task manager (top
) can be used to find the process ID for use in the kill
command.
Using the command line task manager is like a mix between using the kill command directly (since you have to use it eventually anyway) and the graphical task manager (because you get to see what’s running on your Raspberry Pi).
To use the command line task manager to end a program running on a Raspberry Pi:
- Open a new terminal window and type the following command:
top
- Find the process ID (under column PID) for the program you want to kill
- Press CTRL C to exit top
- Type the following command to end the program:
sudo kill -9 process_ID
I’ve found this useful if you’re remotely monitoring your Raspberry Pi. Often when remote monitoring you don’t have a graphical interface available, yet you still want the performance statistics for your Pi (CPU, memory usage etc.). Using top provides you this information along with the process ID to kill errant programs.
If you’re monitoring your Raspberry Pi remotely, I wrote a whole guide on different ways you can monitor a Raspberry Pi, check it out here: chipwired.com/monitor-raspberry-pi
6. Stop a service with systemctl
Systemctl is a set of utility programs that can be used to control services running in the background of your Raspberry Pi. These services can include ssh, performance monitoring, or even a web server if you set one up.
Stopping a service with systemctl is valuable if you’re running a web server on your Raspberry Pi and you want to stop or restart the web server at any stage.
There are a few options depending on what you need to do:
Option 1: Stopping the service
In this option, systemctl commands the background service to stop running. The command is as follows:
sudo systemctl stop service
Replace service
with the name of the service you want to stop. I’ve mostly used this on the web server apache
though I’ve also seen it used on mariadb
.
Option 2: Kill the service
Similar to the other methods in this guide, this option is to send a kill command to the service to get it to stop running. The command is as follows:
sudo systemctl kill -s 2 service
Again where service
is the name of the service you want to kill. I never use this option as the “stop” command usually works for me. If it doesn’t work I use the third option.
Option 3: Kill the service with a higher level
This is the nuclear option to stop the service running at all costs. Usually this isn’t used on services as they are probably doing something important in the background. However, if you do need to do it, this is the command:
sudo systemctl kill -s 9 service
Once again, replace service
with the name of the service you want to kill.
Usually we use the lower kill “level” when stopping a service as it might be doing something important in the background that we can’t see – the lower level will allow it to finish what it’s doing and save important information before exiting. Imagine if an important web page was being served, the lower level will allow the program to finish serving the web page before exiting.