Tuesday, May 9, 2023

Shell Script to Kill All Processes having Specific Pattern/String in Their Name or Description

 What this script for?

This script kills all running processes having a string/pattern "provided by the user" in their name or description.

This script works only on Linux environment, it won't work on Unix.

When you need such script?

Sometimes when I shut down an Oracle database it goes hung in the middle, in many cases it worth to kill all the connected sessions to the DB from OS to release this hung in many cases, so I kill all the processes having this pattern in their name, suppose that my instance name is ORCL: oracleORCL (LOCAL=NO)

This script helps me to safely and easily do this job. This is just an example of usage, but it can kill any process, not only Oracle ones.

Before going to "How to Use" section, please read the following disclaimer carefully:

THIS SCRIPT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY. IT IS PROVIDED "AS IS".
THE AUTHOR WILL NOT BE HELD RESPONSIBLE TO ANY PERSON OR ENTITY WITH RESPECT TO ANY DATA LOSS OR DAMAGES CAUSED BY THIS SCRIPT.

This script is like any powerful Linux command, you suppose to really know what you are doing, make sure you read the prompted messages and outputs carefully.
Also, it's expected that you already practiced/tested this script on a test environment before using it against production.

How to use:

1- You need to provide the full or partial name of the processes you want to kill.
2- The user you are running this script with should be the owner of the processes to be killed.

# sh KILL_ALL_PROCESSES_MATCHING_STRING_LINUX.sh

Read the Disclaimer and decide whether you want to continue or not, then provide the full or partial name of the processes you want to kill:

 

 The script will list you all the processes to be killed along with their total number. [You must review them all carefully before proceeding to the next step, to make sure the script will kill exactly the processes you want]

 

Once you are 100% sure to kill the listed processes, then go ahead and confirm the killing of those processes:

The script will give you 5 seconds in case you want to change your mind, simply use Ctrl+c to terminate it or leave it to run.

Once the killing is complete, the script will check if any other processes are left having the same pattern:

In case there are processes still left, the script will list them to you, you may need to run the script again to kill them, or you may need to execute the script with the right OS user which owns these processes.

You can download the script from this link:

https://www.dropbox.com/s/60ob48dctotmsx1/KILL_ALL_PROCESSES_MATCHING_STRING_LINUX.sh?dl=0

Or just copy the below content to a file with .sh extension to the machine you want to execute it against:


No comments:

Post a Comment