Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save queeup/865c53ef58a841a8746d205314e4eb75 to your computer and use it in GitHub Desktop.
Save queeup/865c53ef58a841a8746d205314e4eb75 to your computer and use it in GitHub Desktop.
Shell Script to Kill All Processes having Specific Pattern/String in Their Name or Description
# #############################################################################################################################################
# This script KILLS all linux processes that match specific pattern provided by the user:
# Ver 1.0
# # # #
# Author: Mahmmoud ADEL # # # # ###
# Created: 08-05-23 # # # # #
# Modified:
# #############################################################################################################################################
# Terminate the Script if the OS is not Linux:
case `uname` in
Linux) true;;
*) echo "This Script works only on Linux OS."; echo "Script Terminated!"; exit;;
esac
export SCRIPT_NAME="KILL_ALL_PROCESSES_MATCHING_STRING_LINUX"
# ##############################
# PROMPT THE DISCLAIMER MESSAGE:
# ##############################
echo ""
echo -e "\033[32;5mTHIS SCRIPT KILLS ALL THE PROCESSES HAVING THE STRING/PATTERN YOU WILL PROVIDE IN THEIR NAME OR THEIR DESCRIPTION. IT EXPECTS THAT YOU REALLY KNOW WHAT YOU ARE DOING. YOU USE IT AT YOUR OWN RISK DUDE!\033[0m"
echo ""
echo "DISCLAIMER: THIS SCRIPT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY. IT IS PROVIDED \"AS IS\"."
echo "********** THE AUTHOR WILL NOT BE HELD RESPONSIBLE TO ANY PERSON OR ENTITY WITH RESPECT TO ANY DATA LOSS OR DAMAGES CAUSED BY THIS SCRIPT."
echo ""
echo "Do you agree to continue? [YES | NO]"
echo "========================="
while read DISCLAIMER
do
case ${DISCLAIMER} in
Y|y|yes|YES|Yes) echo; break;;
N|n|no|NO|No|NE) echo; echo "Nothing wrong with being on the safe side :-)";echo "Please test this script on a test environment first to get more confident with it.";echo "SCRIPT TERMINATED! "; echo; exit;;
*) echo "Please Enter a valid answer: [YES|NO]";;
esac
done
# #####################################
# Prompt the user to enter the pattern:
# #####################################
echo "Enter the FULL or PARTIAL OS Process Name you want to KILL:"
echo "==========================================================="
while read PROCESS_PATTERN
do
case ${PROCESS_PATTERN} in
"") echo "Empty Pattern is NOT allowed. Please Enter a value."; echo;;
*) export PROCESS_PATTERN; echo; break;;
esac
done
# ##################################################
# Print all the services having the entered pattern:
# ##################################################
echo "The following Processes are subject to be killed by this script ..."
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo ""
sleep 1
ps -ef | grep "${PROCESS_PATTERN}" | grep -v grep
export PROCESS_COUNT1=`ps -ef | grep "${PROCESS_PATTERN}" | grep -v grep|wc -l`
echo; echo "Number of Processes to be killed is: ${PROCESS_COUNT1}"
if [[ ${PROCESS_COUNT1} -eq 0 ]]
then
echo
echo "No Processes found having the pattern: ${PROCESS_PATTERN}"
echo ""
echo "Script Terminated!"
echo
exit
fi
# ###########################################
# KILLING Section:
# ###########################################
echo
echo "ARE YOU SURE YOU WANT TO KILL ALL THE ABOVE LISTED PROCESSES? [YES | NO]"
echo "============================================================="
while read CONFIRM_KILL
do
case ${CONFIRM_KILL} in
"YES"|"yes"|"Yes")
echo ""; echo "Attemping to KILL the above listed processes using kill -9 command within 5 seconds ..."; sleep 5
ps -ef | grep "${PROCESS_PATTERN}" | grep -v grep | awk '{print $2}' | xargs -r kill -9
echo ""; echo "Killing attempt Completed!"; echo ""
echo "Checking if any other processes with pattern [${PROCESS_PATTERN}] are still running ..."
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo ""; sleep 3
ps -ef | grep "${PROCESS_PATTERN}" | grep -v grep
export PROCESS_COUNT2=`ps -ef | grep "${PROCESS_PATTERN}" | grep -v grep|wc -l`
if [[ ${PROCESS_COUNT2} -gt 0 ]]
then
echo ""; echo "Number of Processes which are NOT YET KILLED is: ${PROCESS_COUNT2}"
echo ""
echo "You may need to execute this script again to kill those processes, or try to execute it again with a privileged user."
echo "Script Terminated!"
echo
exit
fi
if [[ ${PROCESS_COUNT2} -eq 0 ]]
then
echo "Hooray! ${PROCESS_COUNT1} Process Killed!"
echo
echo "No more processes with pattern [${PROCESS_PATTERN}] can be found."
echo ""
echo "Script Completed."; echo ""
fi
break;;
"No"|"no"|"NO"|"N"|"n") echo "Script Terminated WITHOUT Doing Any Action!";echo ""; break;;
*) echo "Please Enter a VALID answer [YES or NO]."; echo;;
esac
done
# #############
# END OF SCRIPT
# #############
# DISCLAIMER: THIS SCRIPT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY. IT IS PROVIDED "AS IS".
# Do not live under a rock :-) Every month a new version of DBA_BUNDLE get released, download it by visiting:
# http://dba-tips.blogspot.com/2014/02/oracle-database-administration-scripts.html
# REPORT BUGS to: mahmmoudadel@hotmail.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment