How to Kill All Python Processes Except One Specific Script in Linux Bash


2 views

When managing multiple Python processes on a Linux system, there are situations where you need to terminate all Python processes except one critical one. The standard pkill or killall commands don't provide built-in exclusion capabilities, which requires a more sophisticated approach.

Here's a robust method that combines pgrep with process filtering:

# Get PID of the process to keep
KEEP_PID=$(pgrep -f "python your_script_name.py")

# Kill all other python processes
pgrep -f "python" | grep -v "$KEEP_PID" | xargs kill

For systems with GNU grep (supporting negative matching):

pkill -f "python" --inverse --exact "your_script_name.py"

For production environments, consider this more robust version:

#!/bin/bash

TARGET_SCRIPT="your_script_name.py"

# Verify the target process exists
if ! pgrep -f "$TARGET_SCRIPT" > /dev/null; then
    echo "Error: Target script not running"
    exit 1
fi

# Get all python PIDs except our target
PIDS_TO_KILL=$(pgrep -f "python" | grep -vw $(pgrep -f "$TARGET_SCRIPT"))

if [ -z "$PIDS_TO_KILL" ]; then
    echo "No other python processes found"
else
    echo "Killing processes: $PIDS_TO_KILL"
    kill $PIDS_TO_KILL
fi

Consider these additional scenarios:

  • Processes running under different users
  • Virtual environment conflicts
  • Long-running subprocesses

For multi-user systems, add user filtering:

pgrep -f "python" -u $USER | grep -v $(pgrep -f "target.py") | xargs kill

When dealing with hundreds of processes, the ps command might be more efficient:

ps -ef | awk '/python/ && !/your_script_name.py/ {print $2}' | xargs kill

When managing multiple Python processes on a Linux system, you might encounter situations where you need to terminate all running Python scripts except for one critical process. The challenge arises because:

  • Process IDs (PIDs) change between executions
  • Simple pkill commands don't support exclusion patterns
  • You need to preserve a specific script while cleaning up others

Here's a robust approach using standard Linux utilities:

# Kill all Python processes except "my_important_script.py"
pgrep -f "python" | grep -v $(pgrep -f "my_important_script.py") | xargs kill

This command chain works as follows:

  1. pgrep -f "python" - Finds all processes containing "python" in their command line
  2. grep -v $(pgrep -f "my_important_script.py") - Excludes the PID of your target script
  3. xargs kill - Sends the TERM signal to remaining PIDs

For different scenarios, consider these alternatives:

Forceful Termination (SIGKILL)

pgrep -f "python" | grep -v $(pgrep -f "my_important_script.py") | xargs kill -9

Handling Multiple Exclusion Patterns

pgrep -f "python" | grep -v -e $(pgrep -f "script1.py") -e $(pgrep -f "script2.py") | xargs kill

Dry Run Mode (Check Before Killing)

pgrep -f "python" | grep -v $(pgrep -f "my_important_script.py") | xargs echo "Would kill:"
  • Always test with echo first to verify targeted processes
  • Consider adding a delay for graceful shutdowns: ... | xargs kill && sleep 2 && ... | xargs kill -9
  • For production systems, implement process supervision instead of manual killing

If you prefer pkill, this pattern works:

pkill -f "python.*(?!my_important_script)" || true

Note: This uses negative lookahead regex which might not be available in all pkill versions.