shell检查进程是否存在;

 

 

 

http://www.oschina.net/code/snippet_109139_9892

1. [代码]shell脚本循环判断进程是否存在     跳至 [1] [全屏预览]

01 #!/bin/sh
02   
03 while true
04 do
05         process=`ps aux | grep mysqld | grep -v grep`;
06   
07         if [ "$process" == "" ]; then
08                 sleep 1;
09                 echo "no process";
10         else
11                 echo "process exsits";
12                 break;
13         fi
14 done

 

###############################################

http://blog.csdn.net/exbob/article/details/6223239

  1. #test.sh  
  2. #!/bin/sh  
  3. pid=`ps -ef | grep -v grep | grep -v "test.sh" | grep $1 | sed -n  '1P' | awk '{print $2}'`  
  4. if [ -z $pid ] ; then  
  5.     echo "no this process"  
  6. else  
  7.     echo $pid  
  8. fi  

 

================================

http://tangyao97.blog.163.com/blog/static/5903967200972746993/

#!/bin/bash
#判断进程是否存在,如果不存在就启动它如果存在就重启它
PIDS=`ps -ef |grep cameraServer |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then
kill -9 $PIDS
#先关闭进程,在运行此进程
cd /usr/lib/csAppLib
sudo ./cameraServer
#重新运行进程
else
cd /usr/lib/csAppLib
sudo ./cameraServer
#运行进程
fi

---------------

 

 http://hi.baidu.com/neptune_moon/item/00f2c95aaf000fc9d2e10c3f

假设你要杀的进程名字是mydaemon,那么,在脚本里可这样来杀它 #下面两句任选一句 

#pid=`pgrep mydaemon`  pid=`pidof mydaemon`  if [ -n "$pid" ];then   kill -9 $pid
 else   echo "Already Killed!"  fi 依照这个我们可以来编一个kill的脚本,来杀一个指定进程,脚本名为kill.sh,内容为:

#!/bin/bash kill_process() { #下面两句任选一句

#pid=`pgrep mydaemon`

 pid=`pidof $1`  if [ -n "$pid"  ];then   kill -9 $pid  else   echo "already killed~"  fi }

kill_process $1

保存后,我们要先给该脚本赋予可执行权限:

chmod 755 kill.sh

然后,假设要杀的进程名为test,可这样用:

./kill.sh test

就可以杀死test进程了

 ========================================

 

http://www.haogongju.net/art/1676473

#! /bin/bash # author caoxin # time 2012-10-10 # program : 判断进行是否存在,并重新启动 function check(){ count=`ps -ef |grep $1 |grep -v "grep"|wc -l` #echo $count if [ 0 == $count ];then nohup python /runscript/working/$1 & fi } check behaviors.py


参考的网址为: http://blog.csdn.net/shangpusp/article/details/7210446

 

 

http://blog.sina.com.cn/s/blog_6da7c0ba010165fn.html

#!/bin/ksh
 
if [ ! -f $LOCK_FILE ]; then
 
#不存在锁文件,则直接产生锁
echo $$>$LOCK_FILE
 
else
 
#存在琐文件,判断锁文件中所指的PID进程是否存在,直到进程退出停止判断,继续后续执行。
cat $LOCK_FILE|read sh_pid
p_cn=`ps -ef |awk '{if ($2 == '$sh_pid') print $3}'|wc -l`
while (($p_cn))
do
sleep 60
p_cn=`ps -ef |awk '{if ($2 == '$sh_pid') print $3}'|wc -l`
done
#原进程退出,写入新PID加锁
echo $$>$LOCK_FILE
 
fi

 

 

 

 

 http://blog.csdn.net/shangpusp/article/details/7210446

[plain] view plaincopy
 
  1. #!/bin/bash  
  2. #  
  3.   
  4. #调用关闭jboss进程脚本  
  5. stopMethodServer.sh  
  6.   
  7. #打印出当前的jboss进程:grep jboss查询的jboss进程,grep -v "grep" 去掉grep进程  
  8. jmsThread=`ps -ef | grep gdms | grep jboss | grep -v "grep"`  
  9. echo $jmsThread  
  10.   
  11. #查询jboss进程个数:wc -l 返回行数  
  12. count=`ps -ef | grep gdms | grep jboss | grep -v "grep" | wc -l`  
  13. echo $count  
  14.   
  15. sec=7  
  16. #开始一个循环,以判断进程是否关闭  
  17.   
  18. for var in 1 2  
  19. do  
  20.   if [ $count -gt 0 ]; then  
  21.     #若进程还未关闭,则脚本sleep几秒  
  22.     echo sleep $sec second the $var time, the JMS thread is still alive  
  23.     sleep $sec  
  24.   else  
  25.     #若进程已经关闭,则跳出循环  
  26.     echo "break"  
  27.     break  
  28.   fi  
  29. done  
  30.   
  31. #if [ $count -eq 0 ]; then  
  32. # echo "nohup startMethodServer.sh &"  
  33. # nohup startMethodServer.sh &  
  34. #else  
  35. # echo "It's better to check the thread!!!"  
  36. #fi  
  37.   
  38. #调用启动脚本  
  39. nohup startMethodServer.sh & 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2013-06-17 15:43  陳聽溪  阅读(2810)  评论(0)    收藏  举报