Saturday, January 24, 2009

Writing to syslog using shell scripts

logger is a simple command to write a string to /var/log/syslog. For example:

$ logger -i "This is a test string"

$ logger -is -p -f /var/log/syslog This is another test string
    -i includes the PID
    -s sends a copy to stdout (the screen)
    -p is the priority. See the available priorities at 'man logger'
    -f is the log to append to
And here's how to log from a crontab:
* * * * * /usr/bin/logger -i crontab test

Friday, January 23, 2009

Moving Cron jobs to Anacron

Cron is a great way to run recurring jobs. But some jobs need to run weekly...and sometimes the computer is turned off, so the cron job doesn't run. So I'm going to migrate some jobs to anacron. Cron runs once each minute, checking if the time matches anything in the crontab list. Anacron, however, runs once each hour (triggered by cron, restart, or resume) and checks the interval in days since a job was last run.


  • Anacron is a root/sudo-level command. Running it as a user will silently FAIL.
  • Anacron will silently FAIL to run scripts with periods '.' in the filename.
  • Anacron stores the timestamps of each job's last run in /var/spool/anacron/JOBNAME. This is handy to change while testing.

There are two ways to run a command using anacron. You can place the command directly in the anacrontab (/etc/anacrontab), or you can put a script in one of the periodic folders (/etc/cron.daily, /etc/cron.weekly, or /etc/cron.monthly)Here are some examples:

  • For comparison, here's an example cron.daily entry that runs at 07:25 each morning:
    # min hr dom mon dow   command
    25 07 * * * date > /home/YOUR_USERNAME/.cron/test_file_1
  • This /etc/anacrontab entry runs each time anacron is called (days = 0), which is very useful for testing. Start anacron manually with sudo anacron -d.
    #days delay  jobname   command
    0 0 anacron-test2  date > /tmp/crontab-test-ouput-everytime
  • This /etc/anacrontab entry runs once daily (days = 1), no matter how many times anacron is called automatically or manually.
    #days delay  jobname   command
    1 0 anacron-test3  date >> /tmp/crontab-test-output-daily

Friday, January 2, 2009

Web Scraper for string prices

I successfully tested a web scraper in python. It scrapes about 20 web pages for the prices of violin strings, then puts the prices in an OpenOffice document for handy printing. It is structured so I can add other output formats, and I could add an XML file to track prices over time or just print changes.

I'm installing it on the store iMac, and setting it as a daily recurring job. The finished file just pops onto the desktop, marked with the date.

A future version may compare prices from multiple sites.

The script and template live in the standard location for user scripts, /Users/username/Library/Scripts/scriptname/