Blog

03/06/2019

How To Run Cronjob Script On The Last Day Of a Month



How to execute script on the last day of a month on Linux or Unix bash shell? How do I run a disk usage or custom reporting shell/perl/python script on the last day of a month on a Linux or Unix-like systems?

You need to use the date command to find out whether tomorrow is the first day of the next month. If it is true, you can run your script.

Say hello to TZ variable

[donotprint][/donotprint]
TZ is time zone environment variable on Linux or Unix-like systems. The TZ environment variable tells functions such as the ctime(3) family and programs like date what the time zone and daylight saving rule is. For example, Greenwich Mean Time can be defined as follows:

TZ='GMT'

You can set TZ as follows to get tomorrow from the current date (+%d):
TZ=GMT-24 date +%d

How do I find out tomorrow date?

The syntax is as follows:

# CDT
TZ=CDT-24 date +%d
 
#PST
TZ=PST-24 date +%d
 
#EDT
TZ=PST-24 date +%d
 
 
#IST
TZ=IST-24 date +%d

Example: Shell script

#!/bin/bash
# Purpose: Tell if it is last day of a month
# Author: SXI ADMIN <sxi.io> under GPL v2.x+
# ---------------------------------
 
# Find tomorrow day in IST time zone 
day=$(TZ=IST-24 date +%d)
 
# Compare it
# If both are 1 means today is the last day of a month
if test $day -eq 1; then
   echo "Last Day of a Month"
else
   echo "Noop"  
fi

Run it as follows:
$ date
$ ./script

Sample outputs:

Fri Jul 31 12:35:16 IST 2015
Last Day of a Month

Try one more time:
$ date
$ ./script

Tue Aug  4 01:04:48 IST 2015
Noop

Create a wrapper script

Let us say you want to find out disk usage on the last day of a month. You’ve a script called /root/scripts/disk-usage.sh. Modify above script as follows:

#!/bin/bash
# Script: /root/scripts/is-end-of-month.sh
# Purpose: Tell if it is last day of a month
# Author: SXI ADMIN <sxi.io> under GPL v2.x+
# ---------------------------------
 
# Find tomorrow day in IST time zone 
day=$(TZ=IST-24 date +%d)
 
# Compare it
# If both are 1 means today is the last day of a month
if test $day -eq 1; then
    # Call disk usage script
    /root/scripts/disk-usage.sh
fi

Create a cron job

You can install your cronjob by running the following command:
# crontab -e
Append the following code to run a script called /root/scripts/is-end-of-month.sh once a day:

@daily	       /root/scripts/disk-usage.sh

OR

0 0 * * *      /root/scripts/disk-usage.sh

Save and close the file.

This entry is 14 of 15 in the Linux Cron Jobs Howto & Tutorial series. Keep reading the rest of the series:
  1. What is cron on a Linux or Unix-like systems?
  2. HowTo: Add Jobs To cron Under Linux or UNIX?
  3. Linux Verify crond Daemon And Cronjobs Are Running
  4. Linux Start Restart and Stop The Cron or Crond Service
  5. Linux: List / Display All Cron Jobs
  6. Linux / UNIX Crontab File Location
  7. Linux / UNIX: Change Crontab Email Settings ( MAILTO )
  8. Disable The Mail Alert By Crontab Command On a Linux or Unix-like Systems
  9. Linux: At What Time Cron Entries In cron.daily, cron.weekly, cron.monthly Run?
  10. Linux Execute Cron Job After System Reboot
  11. Linux / UNIX Setup and Run PHP Script As A Cron Job
  12. How to run crontab job every minute on a Linux or Unix-like system
  13. Run crontab (cron jobs) Every 10 Minutes
  14. Cron Job Script Execution on the Last Day of a Month
  15. Run crontab (cron jobs) every 1 minute

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...