工作中经常会遇到端口占用导致服务无法启动的情况, 这时候通常需要三步来解决问题:
1. 找到占用端口的 程序pid(netstat -tunlp | grep pid)
2. 通过pid 查看并确定程序(ps aux | grep pid)
3.kill掉该程序,重新启动服务即可 (kill -9 pid)
示例如下:
如查看5140端口的情况,netstat -tunlp |grep 5140
# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 4814/rpcbind
tcp 0 0 0.0.0.0:5908 0.0.0.0:* LISTEN 25492/qemu-kvm
tcp 0 0 0.0.0.0:6996 0.0.0.0:* LISTEN 22065/lwfs
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 38296/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5278/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 5013/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 5962/master
tcp 0 0 0.0.0.0:8666 0.0.0.0:* LISTEN 44868/lwfs
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 22065/lwfs
[root@gy01 jar]# netstat -unlp | grep 5140
udp 0 0 0.0.0.0:5140 0.0.0.0:* 26805/java
关于几个参数的含义
-t (tcp) 仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化为数字
-l 仅列出在Listen(监听)的服务状态
-p 显示建立相关链接的程序名
查看 进程
[root@gy01 jar]# ps -aux | grep 26805
root 20678 0.0 0.0 112676 976 pts/0 S+ 12:00 0:00 grep --color=auto 26805
flume 26805 4.5 9.8 29811516 13009992 ? Sl 12月03 117:25 /home/flume/software/java/bin/java -XX:-UseLoopPredicate -XX:CompileCommand=exclude,org.apache.flume.source.kafka.KafkaSourceManager$KafkaConsumerRunnable::run -Xms20g -Xmx20g -XX:MaxDirectMemorySize=512M -XX:+UseG1GC -Dcom.sun.management.jmxremote -Daudit.properties.conf=/home/flume/software/flume-1.6.1.6/bin/../conf/log_audit.properties -Dfield.map.conf=/home/flume/software/flume-1.6.1.6/bin/../conf/field_map.properties -Dflume.root.logger=INFO,LOGFILE -Dflume.kerberos.enabled=true -cp /home/flume/software/flume-1.6.1.6/conf:/home/flume/software/flume-1.6.1.6/lib/*:/home/flume/software/flume-1.6.1.6/plugins.d/elasticSearch-sink/lib/*:/home/flume/software/flume-1.6.1.6/plugins.d/kafka-source/lib/*:/home/flume/software/hadoop-2.7.2.4/etc/hadoop:/home/flume/software/hadoop-2.7.2.4/share/hadoop/common/lib/activation-1.1.jar:/home/flume/software/hadoop-2.7.2.4/share/hadoop/common/lib/apacheds-i18n-2.0.0-M15.jar:/home/flume/software/hadoop-2.7.2.4/share/hadoop/common/lib/apacheds-kerberos-codec-2.0.0-M15.jar:/home/flume/software/hadoop-2.7.2.4/share/hadoop/common/lib/api-asn1-api-1.0.0-M20.jar:/home/flume/software/hadoop-2.。。。。。。。。。。。。。。。。。。。。。。。。。。
最后kill该进程即可
[root@gy01 ~]# kill -9 20893
贴一个监测端口占用的脚本(来自:http://www.cnblogs.com/wangtao1993/p/6144183.html )
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import socket, time, thread
socket.setdefaulttimeout(3) #设置默认超时时间
def socket_port(ip, port):
"""
输入IP和端口号,扫描判断端口是否占用
"""
try:
if port >=65535:
print u'端口扫描结束'
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result=s.connect_ex((ip, port))
if result==0:
lock.acquire()
print ip,u':',port,u'端口已占用'
lock.release()
except:
print u'端口扫描异常'
def ip_scan(ip):
"""
输入IP,扫描IP的0-65534端口情况
"""
try:
print u'开始扫描 %s' % ip
start_time=time.time()
for i in range(0,65534):
thread.start_new_thread(socket_port,(ip, int(i)))
print u'扫描端口完成,总共用时:%.2f' %(time.time()-start_time)
# raw_input("Press Enter to Exit")
except:
print u'扫描ip出错'
if __name__=='__main__':
url=raw_input('Input the ip you want to scan: ')
lock=thread.allocate_lock()
ip_scan(url)
执行结果如下
# python scan_port.py
Input the ip you want to scan: 20.0.208.112
开始扫描 20.0.208.112
20.0.208.112 : 111 端口已占用
20.0.208.112 : 22 端口已占用
20.0.208.112 : 8000 端口已占用
20.0.208.112 : 15996 端口已占用
20.0.208.112 : 41734 端口已占用
扫描端口完成,总共用时:9.38