Unix Holidays

The /etc/acct/holidays file is intended to be used by various accounting utilities on your system that might need to behave differently on certain days of the year -- holidays. On one of my more recently installed systems, the holiday file looks like this:

* @(#)holidays  January 1, 2008
* Prime/Nonprime Table for UNIX Accounting System
* Curr  Prime   Non-Prime
* Year  Start   Start
  2008  0800    1800
* only the first column (month/day) is significiant.
* month/day     Company
*               Holiday
1/1             New Years Day
7/4             Indep. Day
12/25           Christmas

As the man page for the holidays file format describes, this file is meant to be updated annually and include details on which hours are considered prime time along with which days are considered holidays. I have to be grateful that files like this don't have any say in which days I enjoy a paid day off! But you can easily update the holidays file to reflect holidays that your organization observes.

Another use of the holidays file is to curtail the behavior of cron jobs on days that you don't work. If you send out reminders to staff to clock their hours or turn off the coffee pot before they go home, you might want to avoid doing so on holidays.

To use the holidays file, you first have to make sure it reflects the current year. Federal holidays can be found here. So, say you update the holiday list to look like this:

1/1             New Years Day
1/19            MLK Day
2/16            Washington's birthday
5/25            Memorial Day
7/7             Indep. Day
9/22            Eid al-Fitr starts
9/23            Nici's (my daughter's) birthday
10/12           Columbus Day
11/11           Veterans Day
11/26           Thanksgiving
12/25           Christmas

Now all you have to do is figure out how to make use of the file! Obviously, it isn't heavily used or the out-of-date files on many of my systems would have raised a red flag long before now. But it's a standard file on Solaris, so we might as well use it. Here's a script which shows whether today is a holiday.


grep -v "\*" /etc/acct/holidays > /tmp/$0

MO=`date +%m | sed "s/^0//"`
DAY=`date +%e`

while read dt event
    if [ $dt == $TODAY ]; then
        echo "Yay!  Today is $event!  Go home!  Celebrate!"
done < /tmp/$0

rm /tmp/$0

The sed removal of leading zeroes is included in the fifth line because the date command doesn't have an option that displays the month in numeric form without leading zeroes for January through September. In the sixth line, we use the %e option with the date command to get just the day of the month in the single digit (if applicable) format. Some of the other useful date options are shown below.

opt: %A => Wednesday
opt: %B => September
opt: %C => Tue Sep 23 14:09:49 EDT 2009
opt: %D => 09/23/09
opt: %G => 2009
opt: %H => 14
opt: %I => 02
opt: %M => 09
opt: %R => 14:09
opt: %S => 50
opt: %T => 14:09:50
opt: %U => 38		(week number)
opt: %V => 39
opt: %W => 38
opt: %X => 14:09:50
opt: %Y => 2009
opt: %a => Tue
opt: %b => Sep
opt: %c => Tue Sep 23 14:09:50 2009
opt: %d => 23
opt: %e => 23
opt: %g => 09
opt: %h => Sep
opt: %j => 266
opt: %k => 14
opt: %l =>  2
opt: %m => 09
opt: %p => PM
opt: %r => 02:09:50 PM
opt: %u => 2
opt: %w => 2
opt: %x => 09/23/09
opt: %y => 09
opt: %z => %z

Here's a version of the script that you could refer to within cron jobs to determine whether the cron job should run normally or not:


grep -v "\*" /etc/acct/holidays > /tmp/$0

MO=`date +%m | sed "s/^0//"`
DAY=`date +%e`

while read dt event
    if [ $dt == $TODAY ]; then		# found today's date in the file
        exit 1
done < /tmp/$0

rm /tmp/$0

If the current day is a holiday, this script returns a 1 to warn the script not to run. It returns a 0 if it's a normal day. You could refer to it in a cron job like so:


if [ $? != 0 ]; then

You might come up with other novel ways of using the holidays file. For example, you could come up with a script that tells you whether any holidays fall within the next two weeks. If you're anything like me and don't know about work holidays until one of your coworkers reminds you ("See you next week!" someone says. "Next week? Not tomorrow?" you reply. "Tomorrow's [insert holiday name here]", you workaholic dunce!"). Maybe you'll be the one that sees the holidays coming!

This article is published as part of the IDG Contributor Network. Want to Join?

To express your thoughts on Computerworld content, visit Computerworld's Facebook page, LinkedIn page and Twitter stream.
10 super-user tricks to boost Windows 10 productivity
Shop Tech Products at Amazon