Skip to content
>GLB_
Go back

Automating SQL Script Execution with Cron

In this blog post, we’ll explore how to automate the execution of SQL scripts using cron, a powerful scheduling tool available on Unix-based systems. This approach is ideal for database administrators and developers who need to run SQL scripts at specific intervals without manual intervention.

Overview

Cron jobs allow you to schedule tasks to run at specific times or intervals. By leveraging cron with a script (Bash, Python, etc.), you can automate the execution of SQL scripts against a database such as PostgreSQL, MySQL, SQL Server, or Oracle DB.

Step-by-Step Guide

1. Write the SQL Script

Create the SQL script you want to execute. For example, save the following content as script.sql:

-- script.sql
UPDATE employees SET status = 'active' WHERE last_login > CURRENT_DATE - INTERVAL '30 days';

2. Create a Script to Execute the SQL

Example for PostgreSQL (Bash):

#!/bin/bash
PGPASSWORD="your_password" psql -h localhost -U your_user -d your_database -f /path/to/script.sql

Save this file as execute_sql.sh and make it executable:

chmod +x execute_sql.sh

Example for MySQL (Bash):

#!/bin/bash
mysql -h localhost -u your_user -pyour_password your_database < /path/to/script.sql

3. Test the Script

Before scheduling the task, test the script manually to ensure it works:

./execute_sql.sh

4. Schedule the Task with Cron

Edit the crontab file:

crontab -e

Add a new line to schedule the script. For example, to run the task daily at 3:00 AM:

0 3 * * * /path/to/execute_sql.sh

Save and exit the editor. The cron job is now scheduled!

5. Verify Cron Logs

To confirm the cron job is running, check the cron logs:

grep CRON /var/log/syslog

Advanced Automation with Python

You can also use Python for more flexibility. Below is an example using the psycopg2 library for PostgreSQL:

import psycopg2

def execute_sql():
    try:
        # Connect to the database
        connection = psycopg2.connect(
            host="localhost",
            database="your_database",
            user="your_user",
            password="your_password"
        )
        cursor = connection.cursor()

        # Read and execute the SQL script
        with open("/path/to/script.sql", "r") as file:
            sql_script = file.read()
            cursor.execute(sql_script)
            connection.commit()

        print("Script executed successfully.")
    except Exception as e:
        print(f"Error: {e}")
    finally:
        if cursor:
            cursor.close()
        if connection:
            connection.close()

# Schedule this script using cron

Save the Python script and schedule it with cron similarly:

0 3 * * * /path/to/python3 /path/to/execute_sql.py

Conclusion

Automating SQL script execution with cron is a straightforward yet powerful technique to save time and ensure consistency in database maintenance. Whether you use Bash or Python, the process integrates seamlessly with most database systems.

If you have specific use cases or questions, feel free to share them in the comments below!


Share this post:

Previous Post
Extracting Dynamic Content from an iFrame with Selenium in Python
Next Post
Are Indexes a Good Strategy for Analytical Databases?