How to stop a running LLM in Ollama?

If you're running a Large Language Model (LLM) using Ollama and want to stop it, there are several ways to do so depending on how you started the model. Below are the methods to stop a running LLM in Ollama:


1. Stopping an Interactive Session

If you started the model in an interactive session (e.g., using ollama run <model>), you can stop it by interrupting the process.

Step 1: Interrupt the Process

  • Keyboard Shortcut: Press Ctrl+C in the terminal where the model is running.
    • This will send an interrupt signal to the process, stopping the model and returning you to the command prompt.

Example:

$ ollama run llama2
>>> Hello!
Hello! How can I assist you today?
^C

Pressing Ctrl+C will terminate the session.


2. Stopping a Background Process

If the model is running in the background (e.g., as part of a server or API), you’ll need to identify and stop the process manually.

Step 1: Find the Process ID (PID)

Use the ps command to find the PID of the running Ollama process:

ps aux | grep ollama

This will list all processes related to Ollama. Look for the process that corresponds to the model you’re running.

Step 2: Kill the Process

Once you have the PID, use the kill command to stop the process:

kill <PID>

For example:

kill 12345

If the process doesn’t stop, you can force-kill it using:

kill -9 <PID>

3. Stopping Ollama Serve

If you started Ollama in server mode using ollama serve, you can stop the server by terminating the process.

Step 1: Stop the Server

  • If the server is running in the foreground, press Ctrl+C in the terminal where the server is running.
  • If the server is running in the background, you can stop it by finding and killing the process as described above.

Example:

$ ollama serve
Serving models on http://localhost:11434
^C

Pressing Ctrl+C will stop the server.


4. Using Ollama Commands

Ollama provides commands to manage models and sessions. While there isn’t a direct "stop" command for running models, you can manage models using the following commands:

List Running Models

You can check which models are currently running using:

ollama list

Remove a Model

If you no longer need a model, you can remove it from your system:

ollama rm <model_name>

For example:

ollama rm llama2

This will delete the model from your local storage, effectively stopping any further use of it.


5. Restarting Ollama

If you’re unsure which process is running or want to ensure all Ollama-related processes are stopped, you can restart the Ollama service.

Step 1: Restart Ollama

On most systems, you can restart Ollama using:

sudo systemctl restart ollama

Alternatively, if you installed Ollama via Homebrew (macOS), you can restart it using:

brew services restart ollama

6. Automating Cleanup with Scripts

If you frequently start and stop models, you can create a simple script to automate the process of stopping Ollama.

Example Bash Script:

#!/bin/bash

# Find and kill all Ollama processes
pkill -f ollama

echo "Ollama processes stopped."

Save this script as stop_ollama.sh and run it whenever you want to stop all Ollama processes:

bash stop_ollama.sh

7. Conclusion

To stop a running LLM in Ollama:

  • Use Ctrl+C if the model is running interactively in the terminal.
  • Use ps and kill commands to stop background processes.
  • Restart or reset Ollama using system commands if needed.
  • Remove models using ollama rm if they are no longer required.

By following these steps, you can effectively manage and stop LLMs running via Ollama, whether they are in interactive sessions, background processes, or server mode.