(10 MIN READ)

Python Job Scheduling with Cron

Automate repetitive tasks in Python with Cron, a time-based scheduler. Learn how to schedule Python jobs to run automatically and efficiently with this step-by-step guide.

Written by . Last Updated:

As with any programming language, there are often mundane tasks that developers need to automate.

Python job scheduling is one way to automate repetitive work like generating reports, sending email notifications, or running scripts at specific intervals.

Cron is a popular and widely used utility in Unix-like operating systems that provides a simple way to schedule jobs to run automatically.

In this article, we’ll explore how to schedule Python jobs using Cron and provide some best practices for efficient workload automation.

What is Cron?

Cron, a time-based Python scheduler, is one of the easiest ways to schedule tasks in Linux and macOS environments. 

It allows users to schedule tasks or jobs to run automatically at specified intervals or at specific times. 

The tasks can be simple commands or complex scripts, and the scheduler can be configured to run them at specific times or with specific frequency, such as daily, weekly, or monthly. 

System administrators and programmers use cron to automate repetitive tasks, such as backups, log rotations, database maintenance, and more.

A cron tab is a configuration file containing a list of commands that the cron daemon should execute at specified times. 

The syntax of the cron tab file (either asterisks, values, and ranges) is composed of six fields that specify the schedule for each comman: minute, hour, day of the month, month, day of the week, and the command to be executed.

The cron tab is what actually allows users to schedule repetitive tasks, such as backups or system maintenance, to be run automatically at specific intervals.

What Are Some Python Job Scheduling Use Cases?

Python job scheduling has a wide range of use cases across various industries. One common use case is automating data processing tasks in data science and machine learning applications. For example, scheduling jobs to run Python scripts that retrieve data from APIs or databases, preprocess the data, and apply machine learning algorithms to generate insights.

Also, developers can schedule jobs to run scripts that build, test, and deploy the code on a periodic basis.

Python job scheduling can also schedule periodic backups of critical data, automate report generation, and monitor system health.

How To Schedule a Cron Job

Now that you know the basics of Python job scheduling, you’re ready for our cron job tutorial.

Step 1: Write Your Python Script

The first step is to write the Python script that you want to schedule. For example, let’s say you want to run a script called my_script.py that prints the current time every hour. Here’s what the script might look like:

import datetime 

now = datetime.datetime.now()
print("The current time is:", now)

Save this script to a file in your preferred directory.

Step 2: Set Up the Cron Job

Once you have your Python script, you need to create a cron job to schedule it. Here’s how to do it:

  1. Open the crontab file by running the command crontab -e in your terminal.
  2. If this is the first time you’re creating a cron job, you might be prompted to choose an editor. Choose your preferred editor or leave the default.
  3. Add a new line to the crontab file with the following format:
  4. * * * * * /usr/bin/python /path/to/my_script.py
  5. This line tells cron to run the Python interpreter at every minute of every hour of every day of every month of every year (* * * * *). The second part of the line (/usr/bin/python) specifies the path to the Python interpreter. The third part (/path/to/my_script.py) specifies the path to your Python script.
  6. Save and exit the crontab file.

Step 3: Test Your Cron Job

To test your cron job, wait for the next minute to arrive (or change the cron job to run every minute by changing the first asterisk to a specific number, such as 5 * * * * for every 5th minute of every hour). When the minute changes, your Python script should run and print the current time to the terminal.

If your script doesn’t run, there may be an error in the crontab file or the Python script. Check the log file at /var/log/syslog for error messages.

Step 4: Customize Your Cron Job

You can customize your cron job to run at different intervals by changing the * * * * * part of the crontab line. Here are some examples:

  • Run every hour: 0 * * * *
  • Run every day at midnight: 0 0 * * *
  • Run every Monday at 8:30am: 30 8 * * 1

You can also specify ranges of values or use commas to separate multiple values. For example:

  • Run every weekday at 5pm: 0 17 * * 1-5
  • Run every hour on the half-hour: 30 * * * *

An Alternative Method: Python’s Schedule Library

While both Python Schedule Library and cron jobs can be used for scheduling tasks, there are several reasons why someone might choose to use the Python Schedule Library over cron jobs including:

  • simpler syntax
  • can be used on any Python-supporting operating system
  • more granularity than with cron jobs
  • integrates with other Python libraries
  • written in Python code and can be run in a test environment, making it easier to debug

Here’s an example of how to use Python’s schedule library to schedule a job:

First, you need to install the schedule library by running pip install schedule in your terminal. Then, you can import the library using import schedule.

Next, you need to define the job you want to schedule using the def keyword. For example, you could define a job that prints “Hello, World!” using the following code:

def job():
print("Hello, World!")

Then, you can schedule the job to run every minute using

    schedule.every(1).minutes.do(job)

You can also pass arguments and keyword arguments to your job function by defining them in the function and passing them as arguments to the do() method. For example, if you want to pass a list of names to a job function greet() as an argument, you could define the function like this:

def greet(names):
for name in names:
print(f"Hello, {name}!")
schedule.every(10).minutes.do(greet, ["Alice", "Bob", "Charlie"])

This will run the greet() function every 10 minutes with the list of names as an argument.

You can also schedule a job to run at a scheduled time using schedule.every().day.at("10:30").do(job). This will run the job every day at 10:30 AM.

Lastly, you can run the scheduled jobs using schedule.run_pending() inside a loop, along with a time.sleep() method to avoid using up too much system resources. For example, the following code will run scheduled jobs every second:

while True: 
schedule.run_pending()
time.sleep(1)

Get More from Python

ActiveBatch, as a workload automation platform, integrates with Python to enhance automation capabilities and provide a comprehensive solution for managing workflows across an organization’s IT infrastructure. Here’s how it works:

  1. Python Script Execution: ActiveBatch allows users to execute Python scripts directly within their workflows. Users can incorporate Python scripts into their automation processes to perform specific tasks, such as data manipulation, file processing or interacting with APIs.
  2. Python Integration Modules: ActiveBatch provides integration modules or extensions specifically designed to work with Python. These modules facilitate seamless interaction between ActiveBatch workflows and Python scripts, enabling users to pass data, parameters and results between the two environments efficiently.
  3. Event Triggers: ActiveBatch supports event-driven automation, allowing users to trigger workflows based on various events or conditions. Python scripts can be used to create custom event triggers or handlers, enabling organizations to automate responses to specific events or changes in their IT environment.
  4. Error Handling and Logging: ActiveBatch offers robust error handling and logging capabilities, which can be integrated with Python scripts. Users can incorporate error-handling logic within their Python scripts to handle exceptions and failures gracefully, ensuring the reliable execution of automated processes.
  5. Resource Management: ActiveBatch provides resource management features to optimize resource utilization across distributed environments. Users can leverage Python scripts to interact with ActiveBatch’s resource management APIs, allowing for dynamic allocation and deallocation of resources based on workload demands.
  6. Reporting and Monitoring: ActiveBatch offers comprehensive reporting and monitoring capabilities to track workflow execution, performance metrics, and resource usage. Python scripts can be used to customize reports, analyze data, and generate insights from ActiveBatch’s monitoring data, providing valuable visibility into automation processes.

ActiveBatch’s integration with Python enhances its automation capabilities, allowing organizations to leverage the flexibility, scalability, and extensibility of Python within their automated workflows, achieving greater efficiency, agility, and control of IT operations and business processes.

Conclusion

Python job scheduling with Cron is an easy way to automate simple repetitive tasks such as backups, log rotations, database maintenance, data processing and report generation.

Cron is a widely used utility in Unix-like operating systems that allows users to schedule tasks or jobs to run automatically at specified intervals or times.

If you want event-based job scheduling, error monitoring and handling and smart resource management, you should explore workload automation from ActiveBatch.


Frequently Asked Questions

What is job scheduling in Python?

Python job scheduling entails running tasks or functions at predetermined intervals, and it plays a crucial role in modern applications such as monitoring system health, polling APIs or databases, and auto-scaling. To schedule and execute Python jobs, programmers employ different methods, such as employing simple loops or threaded loops, utilizing the Schedule Python Library, Python Crontab, or utilizing RQ Scheduler as decoupled queues.

Learn how to increase your organization’s productivity and efficiency with ActiveBatch’s advanced batch scheduling capabilities.

What are the problems with Python’s cron tab method for job scheduling?

Python’s cron tab method is a popular option for scheduling recurring tasks in Python. However, it has several drawbacks that limit its usefulness in certain scenarios.

Cron tab is not very flexible when scheduling more complex tasks, especially those that require dynamic scheduling based on external events or conditions.

Also, it can be challenging to manage and monitor cron jobs effectively, especially when dealing with many jobs.

Another limitation is that the cron tab is not well-suited for distributing jobs across multiple machines or nodes, which can limit scalability in distributed systems.

Finally, the cron tab method requires using system-level tools, which may not be available or easily accessible in all environments, such as cloud-based or containerized deployments.

Read more about why ActiveBatch is the best cron job software for your business.

What is Python crontab?

Python Crontab is a useful Python library that enables developers to create schedules for executing Python code like cron jobs. Using this library, you can specify the precise timing for running a specific script or program by setting the interval, time of day, day of the week, or month of the year. The scheduling is achieved by creating rules using the crontab syntax, which determines when a task should be executed.

Discover how ActiveBatch makes automating workflows, scheduling jobs, and managing resources easy.

Can you schedule Python jobs in Windows?

Yes, you can schedule Python jobs in Windows using the built-in Task Scheduler. Additionally, you can use windows job scheduling, a built-in feature of Windows that allows users to schedule tasks to run automatically with Task Scheduler.

Find out more about how to schedule Windows jobs.