Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»General Articles»Schedule a Cron Job at Last Day of the Month

    Schedule a Cron Job at Last Day of the Month

    By RahulJune 27, 20233 Mins Read

    Cron jobs are automated scripts that are essential in system administration and are prevalent in Unix-like operating systems. They allow system administrators and developers to schedule tasks (jobs) to run at specific times. This can be incredibly useful for tasks such as database maintenance, system updates, and data backup. This article will take a deep dive into how to schedule a cron job specifically for the last day of the month, an operation that might seem simple at first but can be surprisingly nuanced.

    Understanding Cron

    Before we delve into scheduling cron jobs, it’s crucial to understand what cron is and how it operates. Cron is a time-based job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run at specific times or on specific days.

    A cron job is the individual task you wish to schedule, and the cron table (crontab) is the configuration file where you define cron jobs. Each line of a crontab file follows a particular syntax to define the schedule and command for a cron job.

    Crontab Syntax

    The typical syntax for a cron job in the crontab file is as follows:

    1
    2
    3
    4
    5
    6
    7
    8
    *     *     *     *     *  command_to_be_executed
    -     -     -     -     -
    |     |     |     |     |
    |     |     |     |     +----- day of the week (0 - 6) (Sunday=0)
    |     |     |     +------- month (1 - 12)
    |     |     +--------- day of the month (1 - 31)
    |     +----------- hour (0 - 23)
    +------------- min (0 - 59)

    The * symbol represents all possible values. For instance, if * is in the ‘hour’ field, the command will be executed every hour.

    Scheduling Cron Jobs for the Last Day of the Month

    The tricky part about scheduling a cron job for the last day of the month is that months have different numbers of days. Not all months have 31 days, and February can have 28 or 29 days depending on whether it’s a leap year or not.

    Here’s how to define a cron job that will run on the last day of the month, at 11:30 PM:

    1
    30 23 28-31 * * [ "$(date +\%m -d tomorrow)" != "$(date +\%m)" ] && your-command

    Let’s break this down:

    • `30 23 28-31 * *` tells cron to run the job at 11:30 PM on the 28th, 29th, 30th, and 31st of any month.
    • The command following this is a small script. It checks if the month of tomorrow’s date (`date +\%m -d tomorrow`) is not equal (`!=`) to the current month (`date +\%m`). If they’re not equal, then the current day is the last day of the month, and the command (`your-command`) will execute.

    Conclusion

    Cron jobs are incredibly flexible and allow you to automate a wide variety of tasks. Scheduling a cron job for the last day of the month is a common use case that can help with tasks such as generating monthly reports or performing month-end backups.

    While scheduling cron jobs may seem daunting at first, once you understand the crontab syntax and the logic behind the scheduling command, it becomes a much more approachable task. As with anything in the realm of system administration, always ensure to test your scripts in a controlled environment before deploying them to production.

    cron crontab
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Using .env File in FastAPI

    Setting Up Email Notifications for Django Error Reporting

    How to Enable Apache Rewrite (mod_rewrite) Module

    View 2 Comments

    2 Comments

    1. Ringo Phonebone on October 5, 2020 10:26 pm

      All correct except that you have to escape the %d.

      59 23 28-31 * * [ “$(date +\%d -d tomorrow)” = “01” ] && /root/script.sh

      Reply
      • Galen Marcel Spikes on March 16, 2021 1:51 pm

        Yeah I was wondering why my jobs weren’t running. Ringo is correct.

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    • How to Enable Apache Rewrite (mod_rewrite) Module
    • What are Microservices?
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.