periodic – using it to run shell scriptsThis article documents how I used periodic to run a shell script every night.
What is periodic?From the manual pages for periodic:
The periodic program is intended to be called by cron(8) to execute shell scripts located in the specified directory.
This program uses files located in the following directories:
/etc/periodic/daily/ /etc/periodic/weekly/ /etc/periodic/monthly/
As indicated by the directory names, periodic is launched on a daily, weekly, and monthly basis. See man periodic for more details.
The shell scriptI had recently installed a www interface to a mailing list archive and now I wanted to automagically update the files instead of manually running the batch job every day.
The first step was to create a shell script which did what I wanted. Here is the contents of /etc/periodic/daily/470.hypermail-adsl:
#!/bin/sh hypermail -p -x \ -m "/usr/local/majordomo/lists/adsl.archive/adsl.archive.9911" \ -c "/usr/local/hypermail/adsl/hmrc.adsl" \ -d "/usr/local/www/data/freebsddiary/adsl/1999_11" \ -b "../"
Note that files within the periodic directories are executed in alphabetical order. This is why the file is prefixed with a number.
For details on hypermail, please see hypermail – creating an www interface to a mailing list archive.
GOTCHA! – TuesdayAfter testing the above script on Monday night to ensure it ran and did what I wanted, I left my box and went to bed, secure in the knowledge that the archives would be updated as I slept. I was wrong. The next morning the archives were untouched. I had no idea why. But checking the file permissions I think I found out why:
-rw-r--r-- 1 root wheel 272 Nov 8 22:31 470.hypermail-adsl
As you can see, the executable bit has not been turned on. Oh well, that’s easy to fix:
chmod 755 470.hypermail-adsl
Now the file looks like this:
-rwxr-xr-x 1 root wheel 272 Nov 8 22:31 470.hypermail-adsl
Here’s hoping that it runs properly tonight.
GOTCHA! (part 2) – WednesdayIt didn’t run again. Hmmm. I asked on IRC for some clues. They suggested path. Of course! I even remember it in the man pages. I can’t use plain old hypermail; I have to include the path. Here is the newly amended script:
#!/bin/sh /usr/local/bin/hypermail \ -p -x \ -m "/usr/local/majordomo/lists/adsl.archive/adsl.archive.9911" \ -c "/usr/local/hypermail/adsl/hmrc.adsl" \ -d "/usr/local/www/data/freebsddiary/adsl/1999_11" \ -b "../"
Now I’m not so sure about the changes I made earlier regarding the executable bit. But at least the files are now all of the same flag setting. Here’s hoping for tonight!
FinallyThe shell script ran last night. I’m pleased. Here’s what I found in my "daily run output" mail message:
Security check: (output mailed separately) Checking for rejected mail hosts: 2 p21-adsl.wn.paradise.net.nz Loading mailbox "/usr/local/majordomo/lists/adsl.archive/adsl.archive.9911"... 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 183 articles. Writing articles to "/usr/local/www/data/freebsddiary/adsl/1999_11"... 0% 3% 5% 8% 11% 14% 16% 19% 22% 25% 27% 30% 33% 36% 38% 41% 44% 47% 49% 52% 55% 58% 60% 63% 66% 69% 71% 74% 77% 80% 82% 85% 88% 91% 93% 96% 99% Writing date index to "/usr/local/www/data/freebsddiary/adsl/1999_11/date.html"... Writing thread index to "/usr/local/www/data/freebsddiary/adsl/1999_11/index.html"... Writing subject index to "/usr/local/www/data/freebsddiary/adsl/1999_11/subject.html"... Writing author index to "/usr/local/www/data/freebsddiary/adsl/1999_11/author.html"...
And this is what was at the bottom of the html page in question:
This archive was generated by hypermail 2.0b3 on Thu 11 Nov 1999 - 02:31:13 NZDT
This script is working just fine. But I’ll monitor it closely by examining using the daily run mail message.
Watch your $PATH environment variable. For security reasons, in scripts (especially ones which deal with user input) you should try and use absolute pathnames.
Hello. Usefull article if somebody will want to run scrips in custom intervals (like hourly or half-day): http://rerepi.wordpress.com/2008/05/24/adding-new-entry-into-etcperiodic/