Skip to content

Instantly share code, notes, and snippets.

@bja2142
Created August 17, 2022 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bja2142/e60cb46b358d74c4801d5ae05fa76c07 to your computer and use it in GitHub Desktop.
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
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