#!/usr/bin/env bash
# -*- coding: utf-8 -*- # as if python :)
# kill process by name for sure
function killer ()
{
### https://github.com/alanfranz/killmesoftly/blob/8dc641a90525265df0fa6fb51c3b4911bbbe5bd9/kms_functions
### http://stackoverflow.com/questions/392022/best-way-to-kill-all-child-processes
PROCESS="$1"
SIGKILL="-9" # "-KILL" / "-TERM"
# | Signal | ID | Action | Description | Java
# | --- | --- | --- | --- | ---
# | SIGHUP | 1 | Terminate | Hangup | The application should reload any config
# | SIGINT | 2 | Terminate | Ctrl-C | Keyboard interrupt, start clean shutdown
# | SIGQUIT | 3 | Terminate | Terminal quit signal | JVM traps and issues a Thread dump
# | SIGABRT | 6 | Terminate | Process abort signal | Do not handle, quit immediately
# | SIGKILL | 9 | Terminate | Kill (forced) | Cannot be trapped
# | SIGTERM | 15 | Terminate | Termination signal. | Quit quickly, safe but fast
PROCESS_PIDS=$(pgrep -x "$PROCESS") # ps -ewwo pid,args | grep chrome | awk '{print $1}' | cut -d' ' -f1 / PROCESS_PIDS=($(pgrep $1))
COUNT=0 # TARGETPIDSCOUNT=$(pgrep -x "$PROCESS" | wc -w)
if [[ "" != "$PROCESS_PIDS" ]]; then
echo -e "now sending SIGTERM to [ $1 ] process ..."
for PID in ${PROCESS_PIDS}; do
kill $SIGKILL $PID &>/dev/null
sleep 0.01 # 1 centisecond
COUNT=$((COUNT+1))
# print in columns
printf "%-5s %-10s\n" "[${COUNT}]" "${PID}"
done
if [[ 0 -eq "$?" ]]; then
if [[ "$COUNT" -gt 1 ]]; then
echo -e "\u25B6 [ $1 ][ $COUNT ] PIDs killed successfully"
else
echo -e "\u25B6 [ $1 ][ $COUNT ] PID killed successfully"
fi
fi
else
echo -e "\u25FC none killed"
fi
return;
}
process killer bash function
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.