Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»Schedule a Cron Job at Last Day of the Month

    Schedule a Cron Job at Last Day of the Month

    RahulBy RahulJune 21, 20192 Mins ReadUpdated:July 28, 2022

    We have a detailed instruction of using crontab on Linux system. This tutorial will help you to configure the cron job to run on every last day of the Month. As there is no direct option available to do it.

    Identify Last Day of Month

    So, first of all, we will schedule cron on the 28,28,29 and 31’st of each month. Now find out if today is the last day of the month. To find it check if the next day is 01’st of the next day and then only execute any command.

    Below command will return the date of the next day.

    date +%d -d tomorrow 
    

    Now check if tomorrow is 01.

     [ "$(date +%d -d tomorrow)" = "01" ] && echo "True" 
    

    If the next day is 01 then above command will print “True” on screen. Here you can use the above script in crontab and change echo with your command.

    Schedule Cron to Run Last Day of Month

    Use the following cron format to run a cron at last day of every month. Here you can change the hours, minutes and script to be executed by cron.

    59 23 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && /root/script.sh
    
    cron crontab
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to check if string contains specific word in PHP
    Next Article How to Install Angular CLI on CentOS/RHEL 8/7/6

    Related Posts

    How to Import GPG Keys on Ubuntu & Debian (without apt-key)

    2 Mins Read

    Running a Cron job every Sunday (Weekly)

    2 Mins Read

    Scheduling a Python Script with Crontab

    2 Mins Read

    Running cron job every 12 hours (twice a day)

    Updated:August 2, 20221 Min Read

    (Resolved) Key is stored in legacy trusted.gpg Keyring

    Updated:August 9, 20222 Mins Read

    How to list all collections in MongoDB database

    1 Min Read

    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

    Recent Posts
    • How to Import GPG Keys on Ubuntu & Debian (without apt-key)
    • How To Install Google Chrome On macOS
    • How to Install Minecraft on Ubuntu 22.04 & 20.04
    • Running a Cron job every Sunday (Weekly)
    • Running Multiple Commands At Once in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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