在Bash中计划Python脚本? [英] Schedule Python Script in Bash?

查看:75
本文介绍了在Bash中计划Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是计划python脚本的新手,需要启动一个脚本,这样它才能运行一两天.我不允许在我的环境中使用cron,因此正在使用此bash脚本.我可以执行什么简单的测试来确保python文件运行?我怎么知道我的文件已运行?而且,如果引发错误,如何确保收到通知?任何帮助,将不胜感激.

I am new to scheduling python scripts and need to start a script so it can run for a day or two. I am not allowed to use cron in my environment so am using this bash script. What is a simple test I can do to make sure the python file runs? How would I know that my file ran? And also, if it throws an error, how can I make sure I am notified? Any help would be appreciated.

#!/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

推荐答案

我添加了一个小片段.

使用变量 TRIGGER ,您可以选择脚本( cron.sh 或您的情况下的课程 cron.py )应选择的日期.第一次开始.

With the variable TRIGGER you can select your date when the script (cron.sh or in your case course cron.py) should be started the first time.

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

WAIT determines the span between on run of the script (cron) and the next run (e. g. 5 minutes or 24 hours) In my example I'm just waiting 5 minutes till the next run.

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

Then we are checking if the Process ID of the started script cron.sh exists und react to this:

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

请注意,这只是一种方法,非常快速又肮脏.这仅应说明如何做到这一点.您可以执行此操作并立即实现自己的功能(通知,日志记录等)

Note that this is just an approach and very quick and dirty. This should only demonstrate how this can be done. You can take this and implement your own functionality now (notifications, logging, etc.)

另外:

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

  • You can check, how often the cron-script has been started. For example set a limit for restarting the cron. If the cron-script fails multiple times in a row, you should deactive this and notify the User.

如果您想在脚本多次失败的情况下收到通知,我个人喜欢发送邮件的选项:

If you want to be notified in case your script has failed multiple times, I personally like the option to send a mail: Send Mail via Terminal

使用数组和循环,可以监视多个Python或bash脚本.

Working with arrays and loops, it's possible to monitor more than one Python- or bash-script.

#!/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 :

This of course also works with a python script. Just change the function startProcess:

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

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

and don't forget to include #!/usr/bin/env python3 in your cron.py file.

使用以下 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

这篇关于在Bash中计划Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆