http://www.cyberciti.biz/faq/unix-linux-bash-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.
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
Sample outputs:
Append the following code to run a script called /root/scripts/is-end-of-month.sh once a day:
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
| Tutorial details | |
|---|---|
| Difficulty | Easy (rss) |
| Root privileges | No |
| Requirements | Bash/KSH/ZSH |
| Estimated completion time | 5m |
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-24date +%d
#PST
TZ=PST-24date +%d
#EDT
TZ=PST-24date +%d
#IST
TZ=IST-24date +%d
Example: Shell script
#!/bin/bashRun it as follows:
# Purpose: Tell if it is last day of a month
# Author: Vivek Giteunder GPL v2.x+
# ---------------------------------
# Find tomorrow day in IST time zone
day=$(TZ=IST-24date +%d)
# Compare it
# If both are 1 means today is the last day of a month
iftest$day -eq 1; then
echo"Last Day of a Month"
else
echo"Noop"
fi
$ date
$ ./scriptSample outputs:
Fri Jul 31 12:35:16 IST 2015Try one more time:
Last Day of a Month
$ date
$ ./scriptTue 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: Vivek Giteunder GPL v2.x+
# ---------------------------------
# Find tomorrow day in IST time zone
day=$(TZ=IST-24date +%d)
# Compare it
# If both are 1 means today is the last day of a month
iftest$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 -eAppend the following code to run a script called /root/scripts/is-end-of-month.sh once a day:
@daily /root/scripts/disk-usage.shOR
0 0 * * * /root/scripts/disk-usage.shSave and close the file.