check-process.sh
1011 Bytes
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
50
51
52
53
#!/bin/bash
while getopts 'n:p:h' OPT
do
case $OPT in
n) name=$OPTARG;;
p) port=$OPTARG;;
h) hlp="yes";;
*) unknown="yes";;
esac
done
# usage
HELP="
usage: $0 [ -n process name -p process port -h ]
-n --> process name < value
-p --> process port < value
-h --> print this help screen
"
hlp=${hlp:=no}
if [ ${hlp} == "yes" ] || [ "${name}" == "" ]
then
echo "${HELP}"
exit 0
fi
function getpid(){
pid=$(sudo ps -ef | grep "${name}" | grep -v grep | grep -v $0 | awk 'NR==1{print $2}' )
}
if [ "${name}" == "" ]
then
exit 0
else
process_flag=$(sudo ps -ef | grep ${name} | grep -v grep | grep -v $0 | wc -l)
fi
if [ "${port}" == "" ]
then
port_flag=1
else
port_flag=$(sudo netstat -lntp | grep ":${port} " | wc -l)
fi
if [ ${process_flag} -ge 1 ] && [ ${port_flag} -ge 1 ]
then
echo "Ok: ${name} is ok,port is ${port}."
exit 0
else
echo "ERROR: ${name} is error,port is ${port}."
exit 2
fi