文章

check_server_process

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash

ss -tnl | awk '{print $4}' | grep -v Local | awk -F":" '{print $NF}' | grep -Ev "22|10022|6379" | sort | uniq > ss_port

[ -f check_server_process.result ] && rm -rf check_server_process.result
touch check_server_process.result
cat ss_port | (
    while read port
    do
        lsof -i:${port} | grep -v COMMAND | awk '{print $2}' | sort | uniq > ${port}_port_pid
                cat ${port}_port_pid | (
                    while read pid
                        do
                            port_command="$(ps -ef | awk '{ if ( $2 ~/'${pid}'/ ) print $0}' | grep -v grep | tr -s ' ' | cut -d' ' -f 8- )"
                            echo "port:${port}|pid:${pid}|${port_command}" >> check_server_process.result
                    done
                )
                rm -rf ${port}_port_pid
    done
)
rm -rf ss_port
本文由作者按照 CC BY 4.0 进行授权