-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindwin32service.sh
executable file
·49 lines (47 loc) · 2.67 KB
/
findwin32service.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
findservice() {
# 根据进程名称或进程ID查找关联的Windows服务;
if [ -z "$1" ]||([[ "$*" == "-h" || "$*" == "--help" ]]);then
[ -z "$1" ] && echo -e "缺少进程名/进程ID!"
echo -e "\`findservice\` 查看某进程关联的Windows服务;"
echo -e "注:(如果存在多个同名进程,请指定pid而不是进程名称,以便于区分!)\n"
echo -e "Usage: findservice CCB_HDZB_2G_DeviceService.exe"
echo -e "\tfindservice 5360"
return 0
fi
local parameter="$1" && shift
expr "$parameter" + 0 &>/dev/null
if [ $? -eq 0 ];then
local serviceInfo=$(cmd /c tasklist /svc /NH /FI "PID EQ ${parameter}"|iconv -s -f GBK -t UTF-8)
else
local serviceInfo=$(cmd /c tasklist /svc /NH /FI "IMAGENAME EQ ${parameter}"|iconv -s -f GBK -t UTF-8)
fi
echo -e "关联服务信息查询:$serviceInfo"
local serviceName=$(echo "$serviceInfo"|tr -s '[\t ]'|grep -i "${parameter:0:25}"|tac|dos2unix -q|awk -F '[\t ]' \
'{srvname="";for(i=3;i<NF;i++){srvname=sprintf("%s %s",srvname,$i)};sub(" ","",srvname);print srvname;exit}') #进程名不区分大小写,目前仅适配关联一个服务的情况,关联多个服务暂不考虑
if [ ! -z "$serviceName" -a ! "$serviceName" = "N/A" -a ! "$serviceName" = "暂缺" ];then
read -p ">> 进程发现关联服务,是否需要停止服务 “$serviceName”? [注:可输入“disabled/demand”停止服务的同时禁用服务或设为手动];"$'\n'"\
Yes/No[y/n]/disabled/demand,默认No: " stopService
if [[ "${stopService,,}" == "y" || "${stopService,,}" == "yes" || "${stopService,,}" == "disabled" || "${stopService,,}" == "demand" ]];then
echo ">>> Stop Service ..."
gsudo net stop "$serviceName"
[[ "${stopService,,}" == "disabled" || "${stopService,,}" == "demand" ]] && gsudo sc config "$serviceName" start= "${stopService,,}"
echo "To find process associated service again ..."
findservice "$parameter"
fi
elif [ ! -z "$serviceName" ];then
local pid
expr "$parameter" + 0 &>/dev/null && pid="$parameter" || \
pid=$(echo "$serviceInfo"|tr -s '[\t ]'|grep -i "${parameter:0:25}"|tac|dos2unix -q|awk -F '[\t ]' '{print $2;exit}') #目前未处理多个同名进程的情况
echo "pid为 $pid 的进程未关联服务或服务不是Win32本地系统服务!"
echo "如:(“Cygwin sshd”是常驻服务,但不是本地服务,是用户登录服务)"
read -p ">> 是否需要终止进程 “PID:$pid”? yes/no(y/n),默认No: " killProcess
if [[ "${killProcess,,}" == "y" || "${killProcess,,}" == "yes" ]];then
echo ">>> Kill Process ..."
gsudo taskkill /F /PID "$pid"
fi
else
echo "没有找到相关进程!"
fi
}
alias findps='findservice'