Ensurity of script execution or kick it if scheduled missed

OS: Ubuntu

Current Scenario: I have a bash script that is scheduled to run daily 12pm, Only one time per day. It fetches users lists from mysql table whose accounts expiry is Today's date & disables them in various mysql table, and sends email alerts as well.

Problem: Sometimes it happens that script miss its schedule either due to server was down, or got rebooted at 12pm.

Therefore I am looking for some logic / method that the script should run every hour and it should check following

If it was executed today successfully at 12pm already , then don't RE-RUN, If it was not executed then then it should execute the script on next run & dont re-run for that date again

1 answer

  • answered 2018-06-25 12:06 Vivek Akupatni

    You need to persist the state of last successful run. I think you can achieve in two ways:

    • Create a new table in the mysql to store this information and store the last datetime when script ran successfully. Every 1hr script wakes up and look at this timestamp to do any pending actions.

    • You could also store this information in some system folder like "/var/log/" or something that your application has access to and you need to sure that this file will not be removed across reboot.

    Have this logic at the beginning of the file:

    last_time_run = <fetch_from_db or persistant file>
    current_time = <get_current_time>
    
    if <already executed>; then
       don't do anything and exit. 
    else
       processed and do normal actions.
    fi
    
    
    # End of the script
    Update the timestamp in table or db to indicate that for current day the processing is finished.