From 9ec27fbd37d2422fb196cd339708ed058425f412 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Tue, 28 May 2024 04:47:45 +0700 Subject: [PATCH] Add userkill --- .gitattributes | 1 + userkill.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 userkill.sh diff --git a/.gitattributes b/.gitattributes index dfe0770..31bb774 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ # Auto detect text files and perform LF normalization * text=auto +*.sh text eol=lf diff --git a/userkill.sh b/userkill.sh new file mode 100644 index 0000000..fa7c869 --- /dev/null +++ b/userkill.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Killing NGINX process as user provided + +if [ -z "$USER" ]; then + echo "\$USER is not provided" + exit 1 +fi + +if [ "$USER" == "root" ]; then + echo "Don't call this on root!" + exit 1 +fi + +# Get the list of processes for the user +user_procs=$(ps -u $USER --forest -o pid=,comm=) + +# Loop through each process +while read -r pid comm; do + # Skip if the command is subcommand or essential features + if [[ "$comm" =~ ^(\\|\|).* || "$comm" == "sh" || "$comm" == "bash" || "$comm" == "docker" || "$comm" == "systemd" ]]; then + continue + fi + + # Kill the process + echo "Killing process $pid ($comm)" + pkill -P $pid + kill -9 $pid + +done <<< "$user_procs" +