Python script that is executed every 5 minutes


Automation and task scheduling play a crucial role in streamlining repetitive tasks in software development. Imagine having a Python script that needs to be executed every 5 minutes, such as fetching data from an API, performing data processing, or sending regular updates. Manually running the script at such frequent intervals can be time-consuming and prone to errors. That's where task scheduling comes in.

In this blog post, we will explore how to schedule the execution of a Python script every 5 minutes, ensuring that it runs automatically without requiring manual intervention. We will discuss different approaches and libraries that can be used to achieve this goal, enabling you to automate your tasks effectively.

Using the time.sleep() Function

One simple approach to running a Python script every 5 minutes is by utilizing the time.sleep() function, which allows us to introduce delays in our script's execution. By combining time.sleep() with a loop, we can create a recurring pattern of execution with a 5-minute interval.

Example 

Here's an example of how this can be achieved 

import time

while True:
   # Perform the desired operations here
   print("Executing the script...")
    
   # Delay execution for 5 minutes
   time.sleep(300)  # 300 seconds = 5 minutes

In this example, we have a while True loop that ensures our script keeps running indefinitely. Inside the loop, we can place the operations that we want to perform every 5 minutes. In this case, we are simply printing a message, but you can replace it with your own code.

The time.sleep(300) statement introduces a 5-minute delay between each iteration of the loop. The argument to time.sleep() is specified in seconds, so 300 seconds correspond to 5 minutes.

By running this script, you will observe that it prints the message every 5 minutes. However, keep in mind that this approach ties up system resources and may not be the most efficient solution for long-running tasks or when precise timing is required.

In the next section, we will explore a more robust and flexible solution using the schedule library, which provides a higher level of control over task scheduling.

Using the Schedule Library

While the time.sleep() approach works well for simple cases, the schedule library provides a more flexible and feature-rich solution for scheduling recurring tasks in Python. It allows us to define more complex schedules and provides additional functionalities such as error handling and logging.

To get started with the schedule library, you need to install it first using pip −

pip install schedule

Once installed, you can import the library and define your scheduled tasks using its API. Let's take a look at an example −

import schedule
import time

def task():
   # Perform the desired operations here
   print("Executing the script...")

# Schedule the task to run every 5 minutes
schedule.every(5).minutes.do(task)

# Run the scheduled tasks indefinitely
while True:
   schedule.run_pending()
   time.sleep(1)

In this example, we define a task() function that represents the operations we want to perform every 5 minutes. We use the schedule.every(5).minutes.do(task) statement to schedule the task to run every 5 minutes.

The schedule.run_pending() function checks if there are any pending tasks to run and executes them. We place it inside a while True loop to continuously check for pending tasks and ensure the script keeps running.

The time.sleep(1) statement introduces a 1-second delay between each iteration of the loop, reducing CPU usage and allowing the script to respond to signals promptly.

With the schedule library, you have more control over scheduling options. You can schedule tasks to run at specific times, on specific days of the week, or even define more complex schedules using the library's rich set of methods.

In the following section, we will explore error handling and other advanced features offered by the schedule library.

Advanced Features and Error Handling

The schedule library offers advanced features that allow you to customize and handle various scenarios in your scheduled script. Let's explore some of these features:

  • Error Handling  When running scheduled tasks, it's important to handle any exceptions that may occur. You can use a try-except block within your task function to catch and handle exceptions appropriately. For example:

def task():
   try:
      # Perform the desired operations here
      print("Executing the script...")
   except Exception as e:
      # Handle the exception here
      print(f"An error occurred: {str(e)}")

By including error handling in your task function, you can gracefully handle any exceptions that may arise during script execution.

  • Logging  Logging is an essential practice for monitoring and troubleshooting your scheduled script. You can use the Python logging module to add logging capabilities to your script. Here's an example of how you can configure logging:

import logging

def configure_logging():
   logging.basicConfig(filename='scheduler.log', level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')

def task():
   try:
      # Perform the desired operations here
      logging.info("Executing the script...")
   except Exception as e:
      # Handle the exception here
      logging.error(f"An error occurred: {str(e)}")

The configure_logging() function sets up the logging configuration, specifying the log file, log level, and log message format. The task() function then uses the logging.info() and logging.error() methods to log informational and error messages, respectively.

  • Flexible Scheduling  The schedule library provides a wide range of scheduling options beyond simple time intervals. You can schedule tasks to run at specific times, on specific days of the week, or even define complex schedules using cron-like expressions. Here are a few examples:

# Schedule task to run every day at 8:30 AM
schedule.every().day.at("08:30").do(task)

# Schedule task to run on Mondays and Fridays at 9:00 PM
schedule.every().monday.and().friday.at("21:00").do(task)

# Schedule task to run every 2 hours on weekdays
schedule.every(2).hours.during(schedule.weekday).do(task)

By leveraging the various scheduling methods provided by the schedule library, you can create more complex and customized schedules for your script.

Using these advanced features, you can enhance the functionality, reliability, and flexibility of your scheduled Python script.

In the next section, we will discuss best practices and considerations for running a Python script every 5 minutes.

Best Practices for Running a Python Script Every 5 Minutes

Running a Python script every 5 minutes requires careful consideration to ensure smooth execution and avoid any potential issues. Here are some best practices to follow −

  • Use a Dedicated Script  Create a dedicated Python script specifically for the task you want to run every 5 minutes. This helps keep your code organized and focused on the specific functionality you need.

  • Implement Proper Error Handling  As mentioned earlier, make sure to include proper error handling in your script. This ensures that any exceptions or errors are caught and handled appropriately. You can use try-except blocks and logging to capture and manage errors effectively.

  • Avoid Lengthy Execution  Keep your script concise and efficient. Running a script every 5 minutes requires it to complete within that time frame. If your script takes longer to execute, consider optimizing it or breaking it down into smaller tasks that can be executed within the given time interval.

  • Avoid Overlapping Execution  Ensure that your script doesn't overlap or interfere with previous instances that are still running. This can be achieved by using a mechanism to check if the previous instance of the script is still running before starting a new one.

  • Monitor and Log Execution  Implement logging and monitoring mechanisms to track the execution of your script. Log relevant information such as start and end times, any errors or exceptions encountered, and other relevant details. Monitoring helps you identify any issues or inconsistencies in the execution process.

  • Consider System Resources  Running a script every 5 minutes requires system resources. Be mindful of the system's limitations, such as CPU and memory usage. If your script consumes significant resources, optimize it to minimize resource usage and avoid any adverse impact on the system's performance.

In the next section, we will provide a complete example of a Python script that is executed every 5 minutes, incorporating the best practices discussed.

Python Script Executed Every 5 Minutes

Now, let's walk through a complete example of a Python script that is executed every 5 minutes. We'll assume that you have already set up the necessary environment and scheduled the script to run periodically using a task scheduler or a cron job.

Example

import time

def run_script():
   # Your script logic goes here
   print("Executing script...")
   # Add your code to perform the desired tasks every 5 minutes

def main():
   while True:
      run_script()
      time.sleep(300)  # Sleep for 5 minutes (300 seconds)

if __name__ == "__main__":
    main()

In this example, we have a run_script() function that represents the logic you want to execute every 5 minutes. This function can include any desired tasks or operations specific to your requirements. In this case, we simply print a message to simulate the execution of the script.

The main() function contains a while True loop, which ensures that the script keeps running indefinitely. Inside the loop, we call the run_script() function, and then use time.sleep(300) to pause the execution for 5 minutes (300 seconds). This effectively schedules the script to run every 5 minutes.

When executing this script, it will continue running and executing the desired tasks every 5 minutes until manually stopped. Ensure that you have set up the necessary scheduling mechanism to invoke the script every 5 minutes.

Remember to customize the run_script() function with your specific logic and tasks that need to be executed periodically.

Conclusion

In this article, we explored how to create a Python script that is executed every 5 minutes. We discussed the importance of scheduling and setting up the environment to ensure the script runs at the desired intervals. We also provided a practical example of a script that demonstrates the execution every 5 minutes.

Automating tasks with scheduled scripts can greatly enhance productivity and efficiency in various domains. By running Python scripts at regular intervals, you can perform repetitive tasks, fetch data, interact with APIs, or perform any other desired actions automatically.

Updated on: 10-Aug-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements