一个简单的启动程序的脚本,执行方法为:./start.sh
program
-
[root@localhost bin]# cat start.sh
-
#!/bin/bash
-
## ====================================================
-
##
-
## 启动程序
-
##
-
-
if [ -z "$1" ]
-
then
-
echo "please input the program you want to start!"
-
exit 1
-
fi
-
-
proc=`ps -ef|grep $1|grep -v grep|wc -l`
-
echo "proc=$proc"
-
if [ $proc -gt 0 ]
-
then
-
echo "$1 is already running!It won't be started repeatedly!"
-
exit 1
-
fi
-
-
echo "nohup ./$1 > /dev/null &"
-
nohup ./$1 > /dev/null &
本来想在启动前判断一下进程是否已经存在,结果明明
program没运行的时候,proc的值却是2,用bash -x也看不出来问题。
干脆屏蔽掉其他代码,只执行
proc=`ps -ef|grep $1`并输出proc,结果如下:
-
[root@localhost bin]# ./start.sh program
-
proc=root 8732 1 0 14:22 pts/0 00:00:02 ./program
-
root 8814 7982 0 14:38 pts/0 00:00:00 /bin/bash ./start.sh program
-
root 8815 8814 0 14:38 pts/0 00:00:00 /bin/bash ./start.sh program
-
root 8817 8815 0 14:38 pts/0 00:00:00 grep program
原来是脚本自身占用了2个名额!查询进程的命令改一下就好了:
-
proc=`ps -ef|grep $1|grep -Ev "grep|start.sh"|wc -l`
阅读(1628) | 评论(0) | 转发(0) |