重启 shell脚本

参考 :http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3602889&page=2

   平时在做系统部署时候经常用到重启APP或web 的sh 脚本,但今天写了个脚本如下 :

pidlist=`ps -ef|grep jenkins | grep -v "grep"|awk '{print $2}'`

echo "jenkins ProcessID List :$pidlist"

if [ "$pidlist" = "" ]

then

  echo "no jenkins alive"

else

  for pid in ${pidlist}

  {

    kill -9 $pid

    echo "KILL $pid:"

    echo "jenkins stop success"

  }

fi

cd /home/uapci/jenkins

nohup /home/uapci/jenkins/bin/startup.sh & > /dev/null

echo "started >"


发现没有达到重启jenkins 的目的,反出现 Killed.说明脚本还没运行完就被KILL掉了,这是由于执行   kill -9 $pid 时将sh 本身kill了,因为我的文件命名为 jenkins_restart.sh,包含了jenkins 关键字,所以在执行 pidlist=`ps -ef|grep jenkins | grep -v "grep"|awk '{print $2}' 一句的时候将自身PID 包含在内,解决方法 :

重命名文件即可,文件名 不要包含grep 过滤的关键字!!

猜你喜欢

转载自xshwlx.iteye.com/blog/2071157