【问题标题】:Execute Multiple shell scripts in Sequence after checking the status of each script检查每个脚本的状态后,按顺序执行多个 shell 脚本
【发布时间】:2019-02-11 03:55:32
【问题描述】:

我们有一个请求以顺序方式执行多个 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

【问题讨论】:

  • 使用合适的进程监控系统——runit、upstart、systemd等;任何这样的系统都可以让您定义启动后挂钩以检查服务是否真的正确运行,以及依赖关系,因此在其他名为dependent 的服务正确运行之前不会启动服务。不要使用 shell 脚本启动服务。
  • 如果您使用的是现代 Ubuntu 或 Red Hat 发行版,请使用 systemd 启动您的服务。它比你可以编造的任何脚本都要强大和功能强大得多。你真的不想重新发明这个轮子。
  • 当你说“完成所有脚本的执行之后”,你的意思是在它们都被启动之后,还是在它们都已经完成运行之后?
  • 运行完@Jack之后

标签: bash shell awk grep sh


【解决方案1】:

您可以使用$! 获取最后一个进入后台的进程的PID。您可以在 if 块中添加任何您想要的内容,以跟踪出错的地方。 wait 命令等待所有后台进程完成。之后您可以打印出您想要的任何内容以进行后期处理。

#!/bin/bash

./script1.sh &
ps $! > /dev/null
if [ $? -ne 0 ]; then
    echo "error pls check";
fi

./script2.sh
ps $! > /dev/null
if [ $? -ne 0 ]; then
    echo "error pls check";
fi

./script3.sh
ps $! > /dev/null
if [ $? -ne 0 ]; then
    echo "error pls check";
fi

wait

【讨论】:

    【解决方案2】:

    您可以使用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[@]}"
    

    【讨论】:

    【解决方案3】:

    这是我想出的并根据我的要求进行了测试

    感谢大家的帮助

    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
    

    【讨论】:

      猜你喜欢
      • 2013-01-05
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2014-05-30
      • 1970-01-01
      • 2015-07-04
      • 2023-02-06
      相关资源
      最近更新 更多