UNIX Hints & Hacks

ContentsIndex

Chapter 1: Topics in Administration

 

Previous ChapterNext Chapter

Sections in this Chapter:

1.1 Collecting System Information

 

1.7 Swap on-the-Fly

 

1.13 Remove the ---- Dashes ----

1.2 Backup Key Files!

 

1.8 Keep It Up with nohup

 

1.14 echo Does ls

1.3 Execution on the Last Day of a Month

 

1.9 Redirecting Output to Null

 

1.15 Building Large Dummy Files

1.4 Dealing with Unwanted Daemons

 

1.10 Keeping Remote Users Out

 

1.16 Burning-in Disk Drives

1.5 Keep Those Daemons Running

 

1.11 Rewinding Tapes Fast

 

1.17 Bringing a System Down

1.6 fuser Instead of ps

1.12 Generating a Range of Numbers

 

 

1.3 Execution on the Last Day of a Month

1.3.1 Description

1.3.1 Description

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.

Example One: The Shell Method

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.

Example Two: The Perl Method

Flavors: AT&T, BSD

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.

Reason

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.

Real World Experience

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.

Other Resources

Man pages:

localtime, tzset, tzfile, crontab

UNIX Hints & Hacks

ContentsIndex

Chapter 1: Topics in Administration

 

Previous ChapterNext Chapter

Sections in this Chapter:

1.1 Collecting System Information

 

1.7 Swap on-the-Fly

 

1.13 Remove the ---- Dashes ----

1.2 Backup Key Files!

 

1.8 Keep It Up with nohup

 

1.14 echo Does ls

1.3 Execution on the Last Day of a Month

 

1.9 Redirecting Output to Null

 

1.15 Building Large Dummy Files

1.4 Dealing with Unwanted Daemons

 

1.10 Keeping Remote Users Out

 

1.16 Burning-in Disk Drives

1.5 Keep Those Daemons Running

 

1.11 Rewinding Tapes Fast

 

1.17 Bringing a System Down

1.6 fuser Instead of ps

1.12 Generating a Range of Numbers

 

 

© Copyright Macmillan USA. All rights reserved.