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
- #test.sh
- #!/bin/sh
- pid=`ps -ef | grep -v grep | grep -v "test.sh" | grep $1 | sed -n '1P' | awk '{print $2}'`
- if [ -z $pid ] ; then
- echo "no this process"
- else
- echo $pid
- 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
http://blog.csdn.net/shangpusp/article/details/7210446
- #!/bin/bash
- #
- #调用关闭jboss进程脚本
- stopMethodServer.sh
- #打印出当前的jboss进程:grep jboss查询的jboss进程,grep -v "grep" 去掉grep进程
- jmsThread=`ps -ef | grep gdms | grep jboss | grep -v "grep"`
- echo $jmsThread
- #查询jboss进程个数:wc -l 返回行数
- count=`ps -ef | grep gdms | grep jboss | grep -v "grep" | wc -l`
- echo $count
- sec=7
- #开始一个循环,以判断进程是否关闭
- for var in 1 2
- do
- if [ $count -gt 0 ]; then
- #若进程还未关闭,则脚本sleep几秒
- echo sleep $sec second the $var time, the JMS thread is still alive
- sleep $sec
- else
- #若进程已经关闭,则跳出循环
- echo "break"
- break
- fi
- done
- #if [ $count -eq 0 ]; then
- # echo "nohup startMethodServer.sh &"
- # nohup startMethodServer.sh &
- #else
- # echo "It's better to check the thread!!!"
- #fi
- #调用启动脚本
- nohup startMethodServer.sh &