≡ Menu

Executing script or command on the last day of a month

Sometime it is necessary to run a command or script on the last day of a month. There is old good hack (I found this somewhere on Usenet) that will decide whether tomorrow is the first day of the next month.

First find out your time zone (such as EDT, PST, IST, GMT etc) using date command:
$ dateOutput:

Wed Jul 12 11:13:41 MST 2006

MST is my time zone. Now type the following command:
$ TZ=MST-24 date +%dOutput:

13

As you see, output is 13 i.e. next day. Let us assume if today is 31st-july-2006 and you type the following command:$ TZ=MST-24 date +%dOutput:

1

Number one (1) indicates that tomorrow is the first day of the next month. Based upon this login you can easily write a shell script:
#! /bin/bash
TOM=$(TZ=MST-24 date +%d)
if [ $TOM -eq 1 ]; then
# commands or script on the last day of a month
# ...
#...
fi

Please note that TZ is the Linux (UNIX) environment variable containing the current time zone identifier (EDT, IST etc). For more information consults the man pages of ctime, date and info pages (info coreutils date) examples section.

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

Comments on this entry are closed.

  • Milton October 12, 2006, 5:45 pm

    Great hack! Another way to actually get the value of the last day of the month is:
    “yest +0 ‘%L'” (See sourceforge.net/projects/yest)

    The utility also solves other date/time calculation problems.

    Milton W.

  • nixCraft October 12, 2006, 5:55 pm

    Milton,

    Thanks for pointing out yest tool.

    Appreciate your post.

  • Heurist March 21, 2008, 2:40 pm

    date -d tomorrow +%d will also output tomorrow’s date without needing to work with the TZ environment.

  • buckeye August 6, 2009, 5:06 am

    i have some questions, can you alter the date and time on an email? an ex-cop/ex blackwater agent is stalking me and sending me emails through yahoo! his and my email accounts are with yahoo! the free account. Anyway, he sent me several treatening emails but the date and time on the emails were different from the actual date and time i recieved them. can the sender manipulate an email date and time stamp before they send it? if so how is he doing this? he was arrested and indicted by the grand jury on stalking and violation of protection order. he is out on bail and I am still getting weir emails from him but the dates and time stamps are wrong on these emails, since he seem to be manipulating the dates and they reflect a past date (prior to PO being filed, the police can’t do anything since the emails do not show current date sent.

    I am scared and confused by all of this, my knowledge of software is very limited and don’t know anyone that has better knowledge on programing. please forgive my spelling, he assaulted me and I had surgery a few days ago, he tore my rotated cuff and injured my neck.

    if anyone can help me out, i will greatly appreciate it.

  • Philippe April 1, 2010, 10:10 pm

    Vivek,
    why don’t you take a useful comment into account ?
    The “date -d tomorrow +%d” tip is preferable, because it is applicable on any Time Zone.