以下两个脚本我分别用SHELL和PYTHON编写,功能相同。
SHELL
- #!/bin/sh
- P_1=”nginx”
- P_2=”mysqld_safe”
- #获取进程ID方法
- processIDget(){
- process=(`ps –ef |grep –i “$1” |grep –v grep |awk ‘{print $2}’`)
- if [ “${process[*]}” != “” ]
- then
- num=${#process[*]}
- for ((i=0;i<$num;i++))
- do
- echo –ne “$1(${process[$i]}\t))”
- done
- echo
- else
- echo –e ne “$1(服务未启)\t”
- echo
- fi
- }
- #开始获取进程ID
- echo –e “192.168.0.1(该有1个$P_1,$P2进程)”
- processIDget $P_1
- processIDget $P_2
PYTHON
- #!/usr/bin/env python
- # -*- coding:iso-8859-15 -*-
- import os
- P_1 = “nginx”
- P_2 = mysqld_safe”
- P = []
- p.append(P_1)
- p.append(P_2)
- print “192.168.0.1(该有1个%s,1个%s进程)”%(P_1,P_2)
- for I in p:
- prcs = os.popen(“ps –ef |grep %s | grep –v”%i).readlines()
- if not prcs:
- print I + “:服务未启”
- else:
- for x in prcs:
- pid = []
- pid = x.strip(“\n”).strip().split()[1]
- print I + “:” + “(” +pid + “)”
阅读(1086) | 评论(0) | 转发(0) |