Skip to content

Instantly share code, notes, and snippets.

@naranyala
Last active August 8, 2023 03:13
Show Gist options
  • Save naranyala/013a6461b419bcef2a456e6a20833584 to your computer and use it in GitHub Desktop.
Save naranyala/013a6461b419bcef2a456e6a20833584 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Function to kill a process
kill_process() {
echo "Killing process with PID: $1"
kill "$1"
}
# Prompt the user for the process name if not provided as an argument
if [ $# -eq 0 ]; then
read -p "Enter the process name: " process_name
else
process_name="$1"
fi
# Get the process ID(s) using pidof
pids=$(pidof "$process_name")
if [ -z "$pids" ]; then
echo "No processes found with the name: $process_name"
exit 1
fi
# Display the process ID(s) and prompt for confirmation to kill
echo "Process(es) found with the name: $process_name"
echo "$pids"
read -p "Do you want to kill the process(es)? [y/N]: " confirm
if [[ $confirm =~ ^[Yy]$ ]]; then
for pid in $pids; do
kill_process "$pid"
done
echo "Process(es) killed successfully."
else
echo "Process(es) not killed."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment