ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
定义服务名称和端口 [root@hadoop-node2 scripts]# cat hadoop_port.log ``` httpd 80 MySQL 3306 Ambari_Server 8080 Ambari_Server 8440 Ambari_Server 8441 Hive_Server2 10000 Hive_Metastore 9083 HDFS_DataNode 50075 HDFS_DataNode 50475 HDFS_DataNode 50010 HDFS_DataNode 50020 HDFS_NameNode_WebUI 50070 HDFS_NameNode_WebUI 50470 HDFS_Secondary_NameNode 50090 HDFS_NameNode_metadata_service 8020 HDFS_NameNode_metadata_service 9000 MapReduce_JobTracker 8021 MapReduce_JobTracker_WebUI 50030 MapReduce_Task_Tracker_WebUI 50060 MapReduce_History_Server_WebUI 51111 HBase_HMaster 60000 HBase_zookeeper 2888 HBase_zookeeper 2181 HBase_Region_Server 60020 HBase_Region_Server 60030 HBase_HMaster_Info_WebUI 60010 Ganglia_Server 8660 Ganglia_Server 8661 Ganglia_Server 8662 Ganglia_Server 8663 Ganglia_Server 8651 WebHCat_Server 50111 ``` 通过数组结合命令检测端口是否存活,存活就打印出来: [root@hadoop-node2 scripts]# cat test_haddop_port.sh ``` #!/bin/bash unset service_name unset service_port service_list_path='/root/scripts/hadoop_port.log' service_list_line=$(cat ${service_list_path}|wc -l) service_list_num=$((${service_list_line} -1 )) service_name=($(awk '{print $1}' ${service_list_path})) service_port=($(awk '{print $2}' ${service_list_path})) main(){ for num in $(seq 0 ${service_list_num}) do service_pro=$(lsof -i:${service_port[${num}]}|wc -l) if [ ${service_pro} -gt 0 ] then echo -e "${service_port[${num}]} \t ${service_name[${num}]}" fi done } main ``` 运行效果: [root@hadoop-node1 scripts]# sh test_haddop_port.sh ``` 80 httpd 3306 MySQL 8080 Ambari_Server 8440 Ambari_Server 8441 Ambari_Server 50075 HDFS_DataNode 50010 HDFS_DataNode 50070 HDFS_NameNode_WebUI 8020 HDFS_NameNode_metadata_service 9000 HDFS_NameNode_metadata_service 2888 HBase_zookeeper 2181 HBase_zookeeper ``` [root@hadoop-node2 scripts]# sh test_haddop_port.sh ``` 3306 MySQL 8441 Ambari_Server 10000 Hive_Server2 9083 Hive_Metastore 50075 HDFS_DataNode 50010 HDFS_DataNode 50090 HDFS_Secondary_NameNode 8020 HDFS_NameNode_metadata_service 2888 HBase_zookeeper 2181 HBase_zookeeper 50111 WebHCat_Server ```