Cron 自动运行 bash 脚本,运行 1 个 bash 脚本,然后运行另一个,加上完整性检查

Cron 自动运行 bash 脚本,运行 1 个 bash 脚本,然后运行另一个,加上完整性检查

所以我有一个由第三方开发的自定义内部应用程序。当应用程序运行时,我可以使用“screen -ls”命令验证它是否正在运行。只要屏幕在 Rails 和 Freeswitch 上运行,我就知道该应用程序已正常启动并运行。

我们有一个特定的 bash 脚本来停止与应用程序相关的服务,还有第二个脚本来启动与应用程序相关的服务。

我的问题是如何将这两个脚本结合起来以重新启动应用程序到 1 个脚本中,其工作方式如下:

  1. 运行脚本 1 - 停止应用程序
  2. 等待应用程序关闭(屏幕进程不再运行)
  3. 运行脚本 2 - 启动应用程序
  4. 等待应用程序启动
  5. 检查“屏幕”插座以确保导轨和自由切换进程正在运行。如果没有,则返回步骤 1 并重复。

现在重新启动应用程序:

  1. 我通过 /tools/stop_app.sh 手动运行停止脚本
    • 然后输出到终端以显示服务关闭。
    • 完成后,它会让我返回到终端提示符。
  2. 现在我通过 /tools/start_app.sh 手动运行启动脚本
    • 这不会输出任何内容,但是一旦完成,它就会返回到终端提示符。
  3. 然后,我运行 screen -ls 来验证该应用程序的所有服务是否正在运行。 (有时 freeswitch 等服务无法启动。)
  4. 如果没有,那么我重新运行停止/启动脚本。

可能有人会问为什么我不将所有内容都放入一个脚本中。这个自定义应用程序非常挑剔,并且由于开发人员的支持有限,我们需要确保使用他们提供的确切工具。因此,1 个脚本调用开发人员提供的 2 个单独的脚本。

通过完整性检查,我指的是检查“屏幕”进程以确保 ruby​​ 和 freeswitch 屏幕正在运行。对于 cron,我想每周自动执行此应用程序重新启动。

请注意,当我说 bash 脚本时,我不确定说 bash 或 shell 是否正确。我没有脚本偏好,只要它是 Ubuntu Linux 中通常默认安装的语言即可。

答案1

你应该能够做这样的事情:

#/usr/bin/env bash

## We will use this function later to check if 
## everything has been correctly started.
function check_if_init_OK {
    ## You will need to edit this to add whatever services
    ## you have to check for.
    c=0; ## This is a counter, initialized to 0

    ## For each of the service names you are interested in.
    ## to add more, just put them after freeswitch, separated by a
    ## space they way they are now (e.g. a b c).
    for service in freeswitch foo bar baz; do

      ## Every time this loop is executed, $service will be
      ## one of the services you put in the list above. The
      ## script will run screen -ls and search for the name of the
      ## service. If it finds it, it will increment the counted $c by one.
      ## That is the meaning of '&&' in bash, x &&y means do y if x 
      ## was successful.
      screen -ls | grep $service >/dev/null 2>/dev/null && let c++; 
    done
    ## This just makes the function return $c which at this point
    ## will be how many of the the services you gave in the list have 
    ## been found.
    echo $c
}

## Run the first script -> stop app
script1.sh &

## Wait until it has stopped running
while screen -ls | grep script1.sh; do sleep 1; done 

## Run the second script -> start app and wait 15 seconds
script2.sh && sleep 15

## Check that everything has started OK. The function
## will return the number of services of interest that
## are up and running. While this is less than the number
## of services of interest, re-run script2.sh and check again.
## This loop will run the check_if_init_OK function until the 
## number returned (the number of running services of interest) is
## 3. You should change the 3 to reflect the actual number of services 
## you are looking for. So, as long as some services have not started,
## run script1.sh and then script2,sh and check if this time eveything
## has started OK. This loop will only exit when everything is working OK.
while [ "$(check_if_init_OK)" -ne 3 ];do
   script1.sh &&  script2.sh
done

相关内容