Skip to content

Instantly share code, notes, and snippets.

@xmaihh
Last active March 25, 2022 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmaihh/6ba1843a7bc473352c262a4051aa7304 to your computer and use it in GitHub Desktop.
Save xmaihh/6ba1843a7bc473352c262a4051aa7304 to your computer and use it in GitHub Desktop.
Close the progress by name.
#!/bin/bash
echo "*********************************"
echo "Close the progress by name!"
echo "usage: ./close_p.sh progressName"
echo "*********************************"
echo
#echo "PID of this script: $$"
#echo "PPID of this script: $PPID"
function MsgShow() {
if [ $# -lt 1 ]; then
echo "Parm err ! Exit the shell!"
exit 1
else
case ${1} in
PARMMISS)
echo "Missing process name to close!"
;;
NOPROSS)
echo "No such procress[$2]!"
;;
MULTIPROSS)
echo "There are too many process contains name[$2]"
;;
*)
echo "Default case!"
;;
esac
exit 1
fi
}
function PidFind() {
PIDCOUNT=$(ps -ef | grep $1 | grep -v "grep" | grep -v $0 | awk '{print $2}' | wc -l)
if [ ${PIDCOUNT} -gt 1 ]; then
MsgShow MULTIPROSS $1
elif [ ${PIDCOUNT} -le 0 ]; then
MsgShow NOPROSS $1
else
PID=$(ps -ef | grep $1 | grep -v "grep" | grep -v ".sh" | awk '{print $2}')
echo "Find the PID of this progress! --- progress:$1 PID=[${PID}] "
fi
read -p "Are you sure you want to close this progress[y/n]: "
if [ $REPLY = "y" ] || [ $REPLY = "yes" ]; then
echo "Kill the process $1 ..."
kill -9 ${PID}
echo "kill -9 ${PID} done!"
else
echo "Confirmation of canceling,exit!"
exit 1
fi
#if we use return ,the rerturn val must between 0 and 255
}
if [ $# -lt 1 ]; then
MsgShow PARMMISS
else
PidFind $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment