1:
编写脚本
#!/bin/bash
##########################################
# Author: panpan.wang
# Desc: kill pid
# Usage: killtask <task_name>
# Date: 2019-05-05
##########################################
# colour macros
if [[ -t 1 ]]; then
NORMAL="$( echo -e "\e[0m" )"
HL_RED="$( echo -e "\e[31;1m" )"
HL_GREEN="$( echo -e "\e[32;1m" )"
HL_BLUE="$( echo -e "\e[34;1m" )"
fi
_hl_red() { echo "$HL_RED""$@""$NORMAL";}
_hl_green() { echo "$HL_GREEN""$@""$NORMAL";}
_hl_blue() { echo "$HL_BLUE""$@""$NORMAL";}
# helper functions
_trace() {
echo $(_hl_blue ' ->') "$@" >&2
}
_notice() {
echo $(_hl_green '==>') "$@" >&2
}
_fatal() {
echo $(_hl_red '==> ERROR:') "$@" >&2
exit 1
}
# ---------------------------------------------------------
if [[ $# -ne 1 ]]; then
_fatal "Usage: killtask <task_name>"
fi
# check if linux process about <task_name> exist
process=`ps -ef | grep $@ | grep -v grep | grep -v $0`
if [[ -z "$process" ]]; then
_notice "Linux process about $@ are not exist."
exit 1
fi
# show all linux process about <task_name>
_notice "ps -ef | grep $@ | grep -v grep | grep -v $0"
_notice "-----------------"
ps -ef | grep $@ | grep -v grep | grep -v $0 | while read process
do
_trace "${process}"
done
# then kill all linux process about <task_name>
ps -ef | grep $@ | grep -v grep | grep -v $0 | awk '{print $2}' | xargs kill -9
_notice "-----------------"
_notice "Kill All Linux Process About $@ Finished..."
2:
编辑 /etc/profile 或 ~/.bashrc 文件,加入如下代码行
//注意你的脚本路径
alias killtask='/home/panpanwang/Desktop/kill.sh'
3:
使用命令 source /etc/profile
或 source ~/.bashrc
,使脚本立即生效
4:执行
//killtask xxx 即可执行,注意florence为你安装的服务名称
killtask florence
5:执行效果
a:服务未启动,执行脚本
b:服务启动,执行脚本
说实话不用这么S,D的操作。
可以直接根据进程name去kill的,有命令的- -,一行代码搞定!!
pkill -f "florence"