Chinaunix首页 | 论坛 | 博客
  • 博客访问: 572186
  • 博文数量: 111
  • 博客积分: 3478
  • 博客等级: 中校
  • 技术积分: 1327
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-28 22:37
文章分类

全部博文(111)

文章存档

2013年(4)

2012年(57)

2011年(15)

2010年(7)

2009年(28)

分类: LINUX

2012-04-27 15:42:54

编写系统进程脚本应注意的问题:

 第一: 不要让脚本名称中含有要监控的进程的名称,否则会造成误判,笔者就曾经因为这个问题造成脚本调试不成功,而浪费了很多时间.

第二: 引用变量的时候符号简单化如能用${variable_name},就不要用“$variable_name"

第三: 调试的时候,为了测试变量值的精确度就把所有的变量的值都用echo显示出来

 第四: 对于条件测试,为了测试条件满足的情况下能否执行相关指令,可以用echo来显示

欢迎大家补充。。。。。。。。。。。。

 

使用示例:

 

#!/bin/bash
#Purpose:This script is used to montor process every 300 sec
#Scpritname: check_mail.sh
#Tips:run this script use this command "source rc.check_mail.sh &" or "./rc.check.mail.sh & "
##############################define variables##################################
process=postfix
process_port_number=:25
process_num=`ps -fe | grep "$process" |grep -v grep  | wc -l`
process_port_num=`netstat -ano|grep "$process_port_number"|grep -v grep |wc -l`
time=`date +%y%m%d-%T`
#############################mian##############################################

while :
do
#time=`date +%y%m%d-%T`
echo "\$process_num is ${process_num},\$process_port_num is ${process_port_num}"
if [ "$process_num" -eq "0" ] && [ "$process_port_num" -eq "0" ];then
echo "${time} ####   $process is not work, it will be restart" >> /var/log/mailpostfix.log
cd /tmp/ 
/etc/init.d/$process start
fi
sleep 300
done

阅读(1173) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

mengchang2012-05-06 13:12:53

恩,我一般都这么用,有时候变量很多,看的眼花缭乱,用这个方法好些

☆彼岸★花开2012-05-06 10:47:46

引用变量的时候符号简单化如能用${variable_name},就不要用“$variable_name"  这个不是什么要求把