下面给大家分享使用python语言实现获取主机名根据端口杀死进程代码。
1
2
3
4
|
ip = os.popen( "ifconfig eth0|grep 'inet addr'|awk -F ':' '{print $2}'|awk '{print $1}'" )
ip = ip..read().strip()
pid = os.popen( "netstat -anp|grep 8998 |awk '{print $7}'" ).read().split( '/' )[ 0 ]
os.popen( 'kill -9 {0}' . format ( int (pid)))
|
下面给大家介绍python语言实现根据pid杀死相应进程,kill_process.py代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import signal
def kill(pid):
try :
a = os.kill(pid, signal.SIGKILL)
# a = os.kill(pid, signal.9) # 与上等效
print '已杀死pid为%s的进程, 返回值是:%s' % (pid, a)
except OSError, e:
print '没有如此进程!!!'
if __name__ = = '__main__' :
kill( 8132 )
ok, Enjoy it !!!
|