shell指令碼監控程序是否存在 不存在則啟動程序

2021-08-18 10:29:35 字數 996 閱讀 1975

#列印出當前的程序,grep -v "grep" 去掉grep程序

ocrthread=`ps -ef | grep ocr.py | grep -v "grep"

`  echo $ocrthread  

#查詢ocr程序個數:wc -l 返回行數

count=`ps -ef | grep exp.py | grep -v "grep"

| wc -l`  

echo $count

sec=7  

#開始乙個迴圈,以判斷程序是否關閉

forvar in 1 2  

doif

[ $count -gt 0 ]; then  

#若程序還未關閉,則指令碼sleep幾秒

echo sleep $sec second the $var time, the ocr thread

is still alive  

sleep $sec  

else

#若程序已經關閉,則跳出迴圈

echo "break"

break

fi  

done  

完整的sh指令碼

#!/bin/bash

sec=1000

while true

doocrthread=`ps -ef|grep extract_info_.py|grep -v 'grep'`

echo $ocrthread

count=`ps -ef|grep extract_info_.py|grep -v 'grep'|wc -l`

echo $count

if [ $count -eq  0 ];then

echo "start process....."

python extract_info_.py

sleep $sec

else

echo "runing....."

fi       

done 

Shell指令碼判斷檔案是否存在

例項一 bin sh 判斷檔案是否存在 這裡的 x 引數判斷 mypath是否存在並且是否具有可執行許可權 if x mypath then mkdir mypath fi 這裡的 d 引數判斷 mypath是否存在 if d mypath then mkdir mypath fi 這裡的 f引數判...

shell指令碼判斷檔案是否存在

檔案不存在則建立 if d data then mkdir data else echo 資料夾已經存在 fi檔案存在則刪除 if f data filename then echo 檔案不存在 else rm f data filename fi判斷資料夾是否存在 if d data then e...

shell指令碼 檢查檔案是否存在

from 寫乙個指令碼,來檢查某個檔案是否存在,如果存在,則輸出它的詳細資訊,如果不存在,則提示輸出檔案不存在。在給出這個指令碼之前,先來了解一下如下幾個命令 檔案upload.zip為例 1.ll h upload.zip rw r r 1 root root 3.3m 06 28 23 21 u...