UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 1: Topics in Administration |
|
||||||||||||||||||||||||||||||||||||
|
This command will determine whether tomorrow is the first day of the next month. If it finds that to be true, you can then execute a predefined script or program.
Flavors: AT&T, BSD
Shells: bsh, bash, ksh, Perl
Syntax:
TZ={GMT|PST|EDT|...}-24 date +%d
TZ is a reserved timezone variable in the UNIX system. Simply tell TZ that it will be tomorrow by adding one day to the current date ( +%d). If today is the last day of the month (that is, 31), TZ will recognize that tomorrow is the first day of the next month.
$ TZ=PST-24 date +%d
This can then be scripted to execute a program if the result of TZ is 1.
#! /bin/sh FILE=`runme` if test `TZ=PST-24 date +%d` = 1; then $FILE fi
Line 1: Sets the shell script to be used (the Bourne shell in this case).
Line 3: Sets a variable to the name of the program to run.
Line 4: If the value that results from TZ equals 1, today is the last day of the month.
Line 5: Because today is the last day, execute the program stored in the variable called FILE.
Line 6: If the result of TZ does not equal 1, end this script.
Shell Script: Perl
Here is a generic approach using a Perl script to achieve the same results as in the previous example. This can be embedded within a larger Perl program so as not to spawn another process.
#!/usr/bin/perl use POSIX; @THE_DATE = localtime (time); ++$THE_DATE[3]; if ((localtime (POSIX::mktime (@THE_DATE)))[3] == 1) { exit 0; } exit 1;
Line 1: Define the script to be a Perl script.
Line 3: Use the POSIX module.
Line 5: Take the local time and fill the array THE_DATE.
Line 6: Add one day to the date.
Line 7: Use mktime to normalize the date in the array THE_DATE. Test whether the day has become the first day of the next month.
Line 8: If it is the first day of the next month, exit with a status of 1.
Line 10: Otherwise, result in an exit status of 0.
There will be times when you'll need to execute programs (such as full backups, batch jobs, filtering of log files, or system utilization) on the last day of the month. At first glance, you might think this is an easy task. Why even use scripts? Why not set up a line in the crontab? Use a script such as this because there is a problem with cron; it allows execution only on any or all days 1-31. But cron cannot determine the last day of each month, so you must find a way to figure it out.
This little hack has come in handy many times. It is very easy to attach it to existing scripts for sending out utilization, disk usage, and user access logs, along with other useful reports, to management and to users on a monthly basis. It is nice to hear users or your boss thanking you for taking the extra time to do the reports each month. Little do they know how easy it is.
Man pages:
localtime, tzset, tzfile, crontab
UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 1: Topics in Administration |
|
||||||||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.