Created
March 19, 2024 06:08
-
-
Save meyt/3ad1ebfe6145f65c5f584058f579f1cc to your computer and use it in GitHub Desktop.
kill recent process of specific user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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