Created
March 5, 2019 03:21
-
-
Save johnnychen94/9291fe83271fe76b562afc3e1eabfbea to your computer and use it in GitHub Desktop.
kill all old listed processes
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 | |
# | |
# kill all old processes listed in GUARDLIST_PROCESS owned by users listed in GUARDLIST_USER | |
# | |
# Usage: | |
# ./process_watchdog GUARDLIST_USER GUARDLIST_PROCESS WHITELIST_USER WHITELIST_PROCESS | |
# | |
# - white list are of highest priority | |
# - processes owned by users listed in WHITELIST_USER will not be killed | |
# - process listed in WHITELIST_PROCESS will not be killed | |
# | |
# user list example: | |
# ```text | |
# user1 comment_of_user1 | |
# user2 comment_of_user2 | |
# ... | |
# userN comment_of_userN | |
# ``` | |
# | |
# process list example: | |
# ```text | |
# python | |
# MATLAB | |
# ``` | |
GUARDLIST_USER=$1 | |
GUARDLIST_PROCESS=$2 | |
WHITELIST_USER=$3 | |
WHITELIST_PROCESS=$3 | |
KILL_TIME=4w | |
while read userinfo; do | |
username=$(echo $userinfo | cut -d' ' -f1) | |
if [[ -z "$(grep $username $WHITELIST_USER)" && ! -z "$(grep ^$username /etc/passwd)" ]]; then | |
while read tokill_process; do | |
if [ -z "$(grep ^$tokill_process $WHITELIST_PROCESS)" ]; then | |
killall --quiet --ignore-case -older-than $KILL_TIME -u $username $tokill_process | |
fi | |
done < $GUARDLIST_PROCESS | |
fi | |
done < $GUARDLIST_USER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment