Skip to content

Instantly share code, notes, and snippets.

@meyt
Created March 19, 2024 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meyt/3ad1ebfe6145f65c5f584058f579f1cc to your computer and use it in GitHub Desktop.
Save meyt/3ad1ebfe6145f65c5f584058f579f1cc to your computer and use it in GitHub Desktop.
kill recent process of specific user
#!/bin/bash
# Usage: killover.sh nginx john 3
# Which means john can have maximum 3 nginx processes, the newest ones must be killed.
cmd=$1
user=$2
max_pids=$3
pids="$(ps --sort=start_time --user "$user" --format pid,cmd | grep "$cmd" | awk '{print $1}')"
pids_count=$(echo "$pids" | wc -l)
forbidden="$(echo "$pids" | tail -n "+$max_pids")"
echo "user: $user"
echo "pids: $pids_count / $max_pids"
for i in $forbidden
do
echo "killing $i..."
kill -9 "$i"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment