Created
August 17, 2022 21:37
-
-
Save bja2142/e60cb46b358d74c4801d5ae05fa76c07 to your computer and use it in GitHub Desktop.
Kill all users who are using more than a fixed limit of process on Linux
This file contains hidden or 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
MAX_PROCESS_LIMIT=10 | |
systemctl status user.slice | | |
egrep "user-[0-9]+\.slice" | | |
sed 's/.*user-\([0-9]*\).*/\1/' | # get uid | |
while read uid; do | |
test $uid -ne 0 && ( # ignore root | |
tasks=$( | |
systemctl status user-${uid}.slice | | |
grep -e Tasks 2>&1 | | |
sed 's/\w*Tasks: \([1-9][0-9]*\) (limit: [0-9]*)/\1/' | |
); | |
test $tasks -gt ${MAX_PROCESS_LIMIT} && pkill -9 -U ${uid} | |
); | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment