【问题标题】:Schedule Python Script in Bash?在 Bash 中安排 Python 脚本?
【发布时间】:2023-03-13 06:12:01
【问题描述】:

我是调度 python 脚本的新手,需要启动一个脚本,以便它可以运行一两天。我不允许在我的环境中使用 cron,所以我使用这个 bash 脚本。我可以做哪些简单的测试来确保 python 文件运行?我怎么知道我的文件运行了?而且,如果它抛出错误,我如何确保我收到通知?任何帮助,将不胜感激。

#!/usr/bin/bash

while true
do
    DATE="$(date +'%d')"
    MONTH="$(date +'%m')"
    YEAR="$(date +'%Y')"
    HOUR="$(date +'%H')"
    MINUTE="$(date +'%M')"
    if ([ "$DATE" = "27" ]) && [ "$HOUR" = "12" ] && [ "$MINUTE" = "30" ]
    then
                 /my_file.py
        sleep 24h
    fi
done

【问题讨论】:

  • 那么,您正在尝试重新实现 cron 的功能,因为您有一个奇怪的理由不使用它?
  • 你认为晚上会发生什么而不是运行你的脚本?您只是在问如何检查退出状态或对退出状态做出反应吗?
  • 考虑在循环中添加一个小睡眠(例如 30 秒)。否则,脚本会消耗大量 CPU。您还确定“/my_file.py”中的工作路径(在根文件夹中?)。

标签: python bash automation scheduling


【解决方案1】:

我添加了一个小sn-p。

使用变量TRIGGER,您可以选择脚本(cron.sh 或在您的情况下是课程cron.py)首次启动的日期。

WAIT 确定脚本运行 (cron) 和下一次运行之间的跨度(例如 5 分钟24 小时)在我的示例中,我我只等了 5 分钟,直到下一次运行。

然后我们检查启动脚本cron.sh的进程ID是否存在并对此做出反应:

  • PID 不可用且返回值不为零:我们需要重新启动脚本。 出现错误。
  • PID 不可用且返回值为零:增加触发器(等待时间)。 没有错误。

请注意,这只是一种方法,非常快速和肮脏。 这应该只演示如何做到这一点。您现在可以使用它并实现自己的功能(通知、日志记录等)

另外:

  • 您可以检查 cron 脚本的启动频率。例如设置重启 cron 的限制。如果 cron 脚本连续多次失败,您应该停用它并通知用户。

  • 如果您想在脚本多次失败时收到通知,我个人喜欢发送邮件的选项:Send Mail via Terminal

  • 使用数组和循环,可以监控多个 Python 或 bash 脚本。

#!/usr/bin/bash

# Select how long between each run should be waited
WAIT="5 minutes"


process_started=false
tmp_pid=0

# For starting a detached process
startProcess() { 
    ./cron.sh &>/dev/null &
    tmp_pid=$(echo $!)
    process_started=true;
    return 0
}


# Make the current date to the staring point
TRIGGER=$(date +'%d-%m-%y-%H-%M') 
echo "Starting Point: $TRIGGER"

while true
do
  # Check for the trigger
  if [ $(date +'%d-%m-%y-%H-%M') = "$TRIGGER" ]; then
    # No process has been stared. Start the proces now
    if [ "$process_started" = false ]; then
      startProcess
      echo "Started script: "  $(date +'%d-%m-%y %H:%M:%S') " (PID $tmp_pid)"
    fi

    # Check for existence
    if ! kill -0 $tmp_pid > /dev/null 2>&1; then
      echo "No process found with pid $tmp_pid" >&2
      # Now, no process has been found. 
      # We can check of the exit code to determine
      # if the process has exited normally
      wait ${tmp_pid}
      if [ $? -eq 0 ]; then
       echo "Script exited normal: " $(date +'%d-%m-%y %H:%M:%S')" . Waiting another $WAIT"
        # Process has been exited normally.
        # Resetting the trigger.
        TRIGGER=$(date +'%d-%m-%y-%H-%M' -d "$WAIT")
        echo "New run in $WAIT: $TRIGGER"
      else
        echo "Script exited with an error"
        # Here. e. g. Start the script again
        startProcess
        echo "Retarted script: "  $(date +'%d-%m-%y %H:%M:%S') " (PID $tmp_pid)"
      fi
    fi
    sleep 1s
  fi
done

这当然也适用于 python 脚本。改函数startProcess就行了:

startProcess() { 
    ./cron.py &>/dev/null &
    tmp_pid=$(echo $!)
    process_started=true;
    return 0
}

不要忘记在您的cron.py 文件中包含#!/usr/bin/env python3


以下cron.sh

x=1
while [ $x -le 5 ]
do
  x=$(( $x + 1 ))
  sleep 1s
done
exit 0

您将收到输出:

Starting Point: 20-10-19-05-22
Started script:  20-10-19 05:22:36  (PID 86402)
No process found with pid 86402
Script exited normal:  20-10-19 05:22:46. Waiting another 5 minutes
New run in 5 minutes: 20-10-19-05-27

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2015-08-18
    相关资源
    最近更新 更多