#!/bin/bash show_help() { echo "Usage: `basename $0` [--grep-str|-g string]" echo "Options:" echo " --help | -h (Optional) Display help information on how to use this script" echo " --grep-str | -g (Optional) Specify a customized string for grepping the process description" } grep_str='8788' while [ "$#" -gt 0 ]; do case "$1" in --help|-h) show_help; exit ;; --grep-str|-g) grep_str="$2"; shift 2;; *) shift ;; esac done function confirm() { while true; do read -p '' yn case $yn in [Yy]* ) return 0;; [Nn]* ) return 1;; [Cc]* ) exit;; * ) echo "Please answer YES, NO, or CANCEL.";; esac done } process_out=`ps -A -U $(whoami) --format pid=,cmd= | grep $grep_str | sed -n '2p'` IFS=' ' read -r p_id p_cmd <<< "$process_out" echo "CMD: \"$p_cmd\"" printf "\n" echo "Kill it? (Yy/Nn/Cc)" if confirm; then kill -9 $p_id echo "[INFO] Process was killed." else echo "[INFO] Process was not killed." fi