如何安装配置Collectd和Collectd-Web监控服务器资源?(1)(4)
第5步:构建自定义Bash脚本,以管理独立式Python服务器
10. 想更轻松地管理独立式PyhtonCGIServer脚本(启动、停止和查看状态),不妨在系统可执行路径下构建下列collectd-server Bash脚本,并使用下列配置:
# nano /usr/local/bin/collectd-server
将下列内容添加到collectd-server文件。
#!/bin/bash
PORT="8888"
case $1 in
start)
cd /usr/local/collectd-web/
python runserver.py 2> /tmp/collectd.log &
sleep 1
stat=`netstat -tlpn 2>/dev/null | grep $PORT | grep "python"| cut -d":" -f2 | cut -d" " -f1`
if [[ $PORT -eq $stat ]]; then
sock=`netstat -tlpn 2>/dev/null | grep $PORT | grep "python"`
echo -e "Server is still running:\n$sock"
else
echo -e "Server has stopped"
fi
;;
stop)
pid=`ps -x | grep "python runserver.py" | grep -v "color"`
kill -9 $pid 2>/dev/null
stat=`netstat -tlpn 2>/dev/null | grep $PORT | grep "python"| cut -d":" -f2 | cut -d" " -f1`
if [[ $PORT -eq $stat ]]; then
sock=`netstat -tlpn 2>/dev/null | grep $PORT | grep "python"`
echo -e "Server is still running:\n$sock"
else
echo -e "Server has stopped"
fi
;;
status)
stat=`netstat -tlpn 2>/dev/null |grep $PORT| grep "python" | cut -d":" -f2 | cut -d" " -f1`
if [[ $PORT -eq $stat ]]; then
sock=`netstat -tlpn 2>/dev/null | grep $PORT | grep "python"`
echo -e "Server is running:\n$sock"
else
echo -e "Server is stopped"
fi
;;
*)
echo "Use $0 start|stop|status"
;;
esac
万一你更改了runserver.py脚本的PORT变量值,就要确保你相应地对该bash文件更改了端口变量。
11. 一旦你构建了collectd-server脚本,添加执行权限,以便能够运行它。现在唯一剩下来的事情就是,以类似管理系统服务的方式来管理Collectd-web服务器,为此执行下列命令。
# chmod +x /usr/local/bin/collectd-server # collectd-server start # collectd-server status # collectd-server stop
Collectd服务器脚本
评论暂时关闭