linux下shell脚本中根据名称查找进程id

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`:`pwd`/libs:`pwd`/exec/0.3.8/
export RUI_BITRATE=1024
export RUI_FRAME_SKIP=2在这里插入代码片

方法一 pidof

targe=`pwd`/"exec/0.3.8/attest"
targe_server=`pwd`/server/nginx

kill -9 `pidof $targe`
kill -9 `pidof $targe_server`




chmod 777 $targe 
chmod 777 $targe_server


方法二 th=ps |grep "$targe"|grep -v "grep"|wc -l

while(true)
do

 th=`ps |grep "$targe"|grep -v "grep"|wc -l`
 if [ $th -eq 0 ];
 then
 	$targe $* &
 fi
 th2=`ps |grep "nginx"|grep -v "grep"|wc -l`
 if [ $th2 -eq 0 ];
 then
 	$targe_server -p `pwd`/server
 fi
 sleep 3 
done

方法三

假设程序运行脚本如下

nohup ./wdt_daemon.sh Rv1126Daemon ./rv1126_daemon &

wdt_daemon.sh的脚本如下:

while [ true ]
do
    time=$(date "+%Y-%m-%d %H:%M:%S")
    pid=`ps | grep "$1"| grep -v grep | grep -v "$0" | grep -v grep |head -n 1| awk '{print $1}'`
    if [ "${pid}" = "" ]
    then
        echo "${time} cannot check $1 process,reboot process" >> "${logFile}"
        cd /mnt/app/web
        nohup $2  >/dev/null 2>&1 &
        sleep 5

        pid1=`ps | grep "$1"| grep -v grep | grep -v "$0" | grep -v grep |head -n 1| awk '{print $1}'`
        if [ "${pid1}" = "" ];then
            echo "${time} re exec $2 fail" >> "${logFile}"
	    cp -rvf /oem/recovery/web /oem/app
        else
            echo "${time} re exec $2 success,pid=${pid1}" >> "${logFile}"
        fi
    else
        echo "${time} $1 running,pid=${pid}" >> "${logFile}"
    fi

        sleep 15
        #check file size
        limit_size_log
done

方法四

## 假设程序名是python promotion
pid=`ps -ef|grep "python promotion"|grep -v grep|awk '{print $2}'`
kill -SIGTERM $pid
echo "process ${pid} killed"

判断程序是否在运行

#! /bin/bash

already_run=`ps -ef|grep "python promotion"|grep -v grep|wc -l`
if [ ${already_run} -ne 0 ];then
    echo "promotionService already Running!!!! Stop it first"
    exit -1
fi

nohup python promotionService.py 8093 &

如果存在,already_run值是1

你可能感兴趣的:(linux,嵌入式硬件,linux)