Schedule Python Script in Bash?

时间秒杀一切 提交于 2021-02-10 05:37:54

问题


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

回答1:


I've added a small snippet.

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 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.

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

  • PID not available and return value not zero: We need to restart the script. There has been an error.
  • PID not available and return value zero: Increment the trigger (waiting time). No error.

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.)

Additionally:

  • 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

  • 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

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
}

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


With the following cron.sh

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

you will receive the output:

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


来源:https://stackoverflow.com/questions/58469601/schedule-python-script-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!