您的位置:首页 > 其它

清理远程桌面软件NX死session留下的进程

2012-06-06 15:31 337 查看
系统:redHat 5.5

背景:开发人员通过远程桌面软件NX连接linux服务器完成他们的设计工作,但是NX有些时候会出现些BUG,开发人员非正常关其PC机、或者其它特殊情况导致其不能恢复以前的连接的NX session. 然而以前的NX session却在服务器端保留,包括在这个session中运行的程序,这样导致服务器资源浪费和产生僵尸进程。

解决方法:运行命令 w -h,分析后发现,每个正常运行的NX session都会记录下用户名、端口号和远程连接IP,而每个正常运行的NX session里开的每个terminal都会关联NX session的端口号。 这样便可以抓出每个用户所有terminal对应的端口号和当前正常的端口号做对比,不同的就判断为有死NX session留下的,将其kill掉。(不好意思,不方便贴图)

使用方法:cron定期运行NXclean.sh将所有用户的死NX session留下脚本全杀掉,只保留当前活动状态的NX session.

具体脚本:NXclean.sh

#!/bin/bash

#
#author:root123.blog.51cto.com
#description: kill all the processes left by dead NX session.
#

logFile=./nxclean.log

#get the users' names who had logined system.
users=$(w -h | awk '{print $1}' | sort -u)

for i in $users
do
#get current NX port number of each user
NXport=$(w -h | grep "^$i" | awk '{print $2}' | grep "^:[0-9]")
NXport=${NXport#":"}
#get all exist NX port number of each user
existPorts=$(w -h | grep "^$i" | awk '{print $3}' | grep "^:[0-9]" | sort -u)
for j in $existPorts
do
if [ X$j =X ]
then :
else
existPort=${j%".0"}
existPort=${j#":"}
#kill all processes which related the existsPort; NX port is greater than or equal 1000
if [ X${existPort != X${NXport} } && [ ${existPort} -ge 1000 ]
then
echo "$(date) $(hostname) user is $i, current NX port is $NXport, dead NX port is $existPort" >> $logFile
echo "kill all the processes which related the dead NX prot $existPort" >> $logFile
#get the PID related $existPort
TTYs=$(w -h | grep  $i | grep $existPort | awk '{print $2}')
for k in $TTYs
do
PIDs=$(ps axu | grep "^$i" grep "$k" | awk '{print $2}')
for l in $PIDs
do
if [ X$l != "X" ]
then
echo "kill process $l" >> $logFile
kill -9 $l
fi
done
done
fi
fi
done
done

注:脚本为手动输入,如有错,请通知我更正。
本文出自 “简单至上” 博客,请务必保留此出处http://root123.blog.51cto.com/3109439/890265
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: