检查每个脚本的状态后,依次执行多个Shell脚本

时间:2018-09-05 23:45:14

标签: bash shell awk grep sh

我们要求以顺序方式执行多个Shell脚本(启动应用程序服务器的脚本)

真正的选择是,我们必须在继续下一个脚本之前验证进程(pid)是否已启动。如果没有进程在运行,则显示错误消息并继续下一个脚本

在完成执行(运行)所有脚本后,还会发送错误消息(该脚本失败)的合并电子邮件

注意:这些服务不仅仅取决于我们正在检查每个脚本之后的状态

下面是我想出的... 请帮助

 #!/bin/bash

./script1.sh
PID=`ps -ef | grep c3f | grep -v grep | awk '{print $2}' | wc -l`
if [ $PID -ne 0 ]; then
        echo "error pls check";
 fi

./script2.sh
PID1=`ps -ef | grep d3f | grep -v grep | awk '{print $2}' | wc -l`
if [ $PID1 -ne 0 ]; then
        echo "error pls check";
 fi

./script3.sh
 PID2=`ps -ef | grep E3f | grep -v grep | awk '{print $2}' | wc -l`
if [ $PID2 -ne 0 ]; then
        echo "error pls check";
 fi

3 个答案:

答案 0 :(得分:1)

您可以使用<\br>来获取最后一个进入后台的进程的PID。您可以在这些<div class="v-slot"> <div class="v-label v-widget v-label-undef-w"> CIABC-Idq BLA HLA N1 Dd/Coind <br> (BLen AccBLA HLA) </div> </div> 块中添加所需的任何内容,以跟踪出了哪些问题。 $!命令等待所有后台进程完成。之后,您可以打印出所需的任何内容以进行后处理。

if

答案 1 :(得分:1)

例如,您可以使用pgrep命令按名称查找进程

查找名称包含c3f的进程:

pgrep c3f

查找完整命令行包含字符串c3f的进程

pgrep -f c3f

我们可以重新编写脚本,如下所示:

#!/bin/bash

declare -a PROCESSES_NOT_FOUND=()

./script1.sh
if ! pgrep -f c3f; then
    PROCESSES_NOT_FOUND+=(c3f)
    echo "Not found c3f, error pls check";
fi

./script2.sh
if ! pgrep -f d3f; then
    PROCESSES_NOT_FOUND+=(d3f)
    echo "Not found d3f, error pls check";
fi

./script3.sh
if ! pgrep -f E3f; then
    PROCESSES_NOT_FOUND+=(E3f)
    echo "Not found E3f, error pls check";
fi

echo "Not found these processes: ${PROCESSES_NOT_FOUND[@]}"

或者我们可以使用数组来存储进程和脚本的映射

#!/bin/bash

declare -A PROCESS_MAP
PROCESS_MAP[c3f]=./script1.sh
PROCESS_MAP[d3f]=./script2.sh
PROCESS_MAP[E3f]=./script3.sh

declare -a PROCESSES_NOT_FOUND=()

for PROCESS in "${!PROCESS_MAP[@]}"; do
    "${PROCESS_MAP[$PROCESS]}"
    if ! pgrep -f "$PROCESS"; then
        PROCESSES_NOT_FOUND+=("$PROCESS")
        echo "Not found $PROCESS, error pls check";
    fi
done

echo "Not found these processes: ${PROCESSES_NOT_FOUND[@]}"

答案 2 :(得分:0)

这就是我想出并测试了我的要求

谢谢大家的帮助

testC1.sh

#!/bin/sh

today=`date +%m%d`
err=0

./testCC.sh start s1a cc
PID1=`ps -ef|grep -E 'c35f.*s1a' | grep -v grep | awk '{print $2}'`
if [ "$PID1" -eq 0 ] || [ -z "$PID1" ]; then
        # PROCESS s1a not started
        err=$(( err + 1))
else
        echo "$(date) - Process s1a with $PID1 is running " > /tmp/log_"$today".log
fi

./testCC.sh start s1b cc
PID2=`ps -ef|grep -E 'c35f.*s1b' | grep -v grep | awk '{print $2}'`
if [ "$PID2" -eq 0 ] || [ -z "$PID2" ]; then
        # PROCESS s1b not started
        err=$(( err + 2))
else
        echo "$(date) - Process s1b is running: $PID2 " >> /tmp/log_"$today".log
fi

./testCC.sh start s2a cc
PID3=`ps -ef|grep -E 'c35f.*s2a' | grep -v grep | awk '{print $2}'`
if [ "$PID3" -eq 0 ] || [ -z "$PID3" ]; then
        # PROCESS s2a not started
        err=$(( err + 3))
else
        echo "$(date) - Process s2a with $PID3 is running " > /tmp/log_"$today".log
fi
./testCC.sh start s2b cc
PID4=`ps -ef|grep -E 'c35f.*s2b' | grep -v grep | awk '{print $2}'`
if [ "$PID4" -eq 0 ] || [ -z "$PID4" ]; then
        # PROCESS s2b not started
        err=$(( err + 4))
else
        echo "$(date) - Process s2b is running: $PID4 " >> /tmp/log_"$today".log
fi


if (( $err > 0 )); then
        # identify which PROCESS had the problem.
        if (( $err == 1 )); then
                condition="PROCESS s1a is down"
        elif (( $err == 2 )); then
                condition="PROCESS s1b is down"
        elif (( $err == 3 )); then
                condition="PROCESS s2a is down"
        elif (( $err == 4 )); then
                condition="PROCESS s2b is down"
        else
                condition="Process didnt started properly..Please Check"
        fi

#  send an email to get eyes on the issue

echo "$condition on $(hostname)
on $(date)\n please login to the server to check the process" | mail -s "Alert  Daily Bounce" emailid@corp.com

else

echo "Script on $(hostname) at $(date) sucessfully implemented" | mail -s "Notice  Daily Bounce"  -r 'emailid@corp.com' emailid@corp.com

fi