Chinaunix首页 | 论坛 | 博客
  • 博客访问: 425468
  • 博文数量: 126
  • 博客积分: 35
  • 博客等级: 民兵
  • 技术积分: 1262
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 16:39
文章分类

全部博文(126)

文章存档

2017年(2)

2016年(20)

2015年(64)

2014年(24)

2013年(16)

我的朋友

分类: Python/Ruby

2014-03-12 14:52:27

一个简单的启动程序的脚本,执行方法为:./start.sh  program
  1. [root@localhost bin]# cat start.sh
  2. #!/bin/bash
  3. ## ====================================================
  4. ##
  5. ## 启动程序
  6. ##

  7. if [ -z "$1" ]
  8. then
  9.     echo "please input the program you want to start!"
  10.     exit 1
  11. fi

  12. proc=`ps -ef|grep $1|grep -v grep|wc -l`
  13. echo "proc=$proc"
  14. if [ $proc -gt 0 ]
  15. then
  16.  echo "$1 is already running!It won't be started repeatedly!"
  17.  exit 1
  18. fi

  19. echo "nohup ./$1 > /dev/null &"
  20. nohup ./$1 > /dev/null &
本来想在启动前判断一下进程是否已经存在,结果明明program没运行的时候,proc的值却是2,用bash -x也看不出来问题。
干脆屏蔽掉其他代码,只执行proc=`ps -ef|grep $1`并输出proc,结果如下:
  1. [root@localhost bin]# ./start.sh program 
  2. proc=root 8732 1 0 14:22 pts/0 00:00:02 ./program
  3. root 8814 7982 0 14:38 pts/0 00:00:00 /bin/bash ./start.sh program
  4. root 8815 8814 0 14:38 pts/0 00:00:00 /bin/bash ./start.sh program
  5. root 8817 8815 0 14:38 pts/0 00:00:00 grep program
原来是脚本自身占用了2个名额!查询进程的命令改一下就好了:
  1. proc=`ps -ef|grep $1|grep -Ev "grep|start.sh"|wc -l`


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