shell判斷進程是否存在

#!/bin/bash

#進入rest全部目錄,後續在此目錄執行動做
cd /data/www/vhosts/go/

#rest的進程數,因爲腳本名稱爲restart_rest.sh,因此排除了對restart_rest的搜索
n=`ps -ef|grep rest|grep -v grep|grep -v restart_rest|wc -l`

if [ 0 -eq $n ]; then
    #進程不存在
    echo "rest進程不存在"
    #nohup ./rest  > /dev/null 2>&1 &
else
    #rest進程存在,殺掉進程
    echo "rest進程已存在,殺掉進程"
    ps -ef|grep rest|grep -v grep|grep -v restart_rest|awk '{print $2}'|xargs kill -9
fi
echo "rest啓動服務"
#使用nohup在後臺執行服務 nohup ./rest > /dev/null 2>&1 &
相關文章
相關標籤/搜索