免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1182 | 回复: 1

我的脚本哪里出了错? [复制链接]

论坛徽章:
0
发表于 2005-07-20 00:42 |显示全部楼层
公司的mail服务器是redhat 9的。这台老机器经常出一些莫名的故障,比如,邮件服务意外中止。通常的方法是执行/etc/rc.d/rc3.d/S80sendmail start就应该将这个服务启动。但是这台机器经常要多次启动这个程序才能成功。有时甚至十几次,几十次。为了避免手工多次启动的麻烦,我写了一个自动重启动的脚本,这个脚本命名为startmail,放在/root目录下。内容如下:
#!/bin/sh
cat timpppp |xargs kill     #杀掉上一个进程
/etc/rc.d/rc3.d/S80sendmail start >./temp     #启动邮件服务,将结果保存
cat temp |grep OK
if [ "$?" -eq 0 ]     #测试服务是否正常启动
then               #成功发出通知,并清理临时文件
echo "Mail service was started OK!"
rm timpppp -f
rm temp -f
else            #不成功则重新调用该程序
echo "lease wait! Mail service is now being started !"
ps -ef |grep startmail |grep -v grep    #显示这个程序的进程号
ps -ef |grep startmail |grep -v grep|awk '{print $2}' >timpppp    #保存当前进程号
cat timpppp     #显示当前进程号,供测试观察
/root/startmail                #重新调用该程序
fi

但是文件执行的结果出乎我的意料,是这样的:
Please wait! Mail service is now being started !
root     17006     1  0 00:29 pts/4    00:00:00 sh /root/startmail
17006
17039
17040
17041
17042
kill 17039: No such process
kill 17040: No such process
kill 17041: No such process
kill 17042: No such process
Please wait! Mail service is now being started !
root     17044     1  0 00:30 pts/4    00:00:00 sh /root/startmail
17044
17078
17079
17080
kill 17078: No such process
kill 17079: No such process
kill 17080: No such process


这可把我吓出一身冷汗!
幸好这个进程是不存在的,要是恰好有一个这个的进程存在,它岂不被我误杀了?
那些No such process的进程是如何进入临时文件timpppp的呢?我不知道我的脚本哪里出了错。
我曾尝试将脚本修改,将最后几行改成:
echo "lease wait! Mail service is now being starting !"
ps -ef |grep startmail |grep -v grep|grep -v vi
rm timpppp -f
ps -ef |grep startmail |grep -v grep|grep -v vi |awk '{print $2}' >timpppp
cat timpppp
/root/startmail
fi

但还是不能解决问题,程序跑开始几个进程还行,到了后来,照样出同样的错误。到底是哪里出了错?请大家指点!
谢谢!

论坛徽章:
0
发表于 2005-07-20 05:49 |显示全部楼层

我的脚本哪里出了错?

逻辑上有点问题,不要杀上一次保存的进程id(cat timpppp |xargs kill  )如果是第一次运行这个脚本就一定会出错,运行一段时间后在啥进程也可能误杀掉别的进程。
为什麽不直接用ps 取出id 后kill呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP