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 local0.info -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.

Tips

  • 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/

Sunday, December 21, 2008

Installing Skype on an XO laptop

I successfully installed Skype with video and did a test call from my XO laptop (build 720) today. I used a version of these instructions. Note: Skype will probably need to be reinstalled each time the xo laptop is upgraded. Warning: Skype isn't really meant for the xo - this install creates several zombie processes each time Skype is started, and the sound quality is definitely inferior.

  1. Install Skype
    $su
    #mkdir /home/olpc/skype
    #cd skype
    #wget http://skype.com/go/getskype-linux-fc7
    #yum --nogpgcheck -y localinstall skype-2.0.0.72-fc5.i586.rpm
    #wget ftp://ftp.pbone.net/mirror/atrpms.net/el5-i386/atrpms/testing/#libasound2-1.0.15-33.el5.i386.rpm
    #rpm -i libasound2-1.0.15-33.el5.i386.rpm
    #wget http://dev.laptop.org/~ffm/gstfakevideo.zip
    #unzip gstfakevideo.zip
    #chmod +x gstfakevideo
    
  2. Skype doesn't have a sugar package, so it must be started by root with the command /home/olpc/skype/gstfakevideo. This is cumbersome and hard to remember, so use nano .bashrc to create an alias. Add this line to the 'alias' section of the .bashrc.
    # User specific aliases and functions
    alias skype 'sudo /home/olpc/skype/gstfakevideo'
    
    Now the user (not root) can start Skype from any prompt with the command skype.

SSH suddenly stops working

I tried to ssh to my XO laptop today, but got the following error:

me:~$ssh user@(IP addr of xo_laptop)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
blah:blah:blah:blah:blah:blah:blah:blah:blah:blah.
Please contact your system administrator.
Add correct host key in /home/USER/.ssh/known_hosts to get rid of this message.
Offending key in /home/USER/.ssh/known_hosts:NUMBER
RSA host key for (IP addr of xo_laptop) has changed and you have requested strict checking.
Host key verification failed.
me:~$

Why? Probably because I upgraded the OLPC laptop.

How to fix it?

  1. Use ssh-keygen -R (IP addr) to purge the old key.
  2. Connect again like normal ssh user@xo_laptop to install the new (current) key.

Monday, October 13, 2008

Creating a patch to fix an Ubuntu bug

Today I'm preparing a patch to fix Launchpad Bug #117984, which is also Gnome Bug #451734.

References: Ubuntu Wiki, Ubuntu Packaging Guide

It turns out that to do it properly, you need to make two patches. The first one is a .diff file for Debian and upstream. The second creates a .debdiff patch for Ubuntu.

There are circumstances, of course, where one of the patches is not neccessary, and some of these steps can be skipped.

    Note: Use steps 1-9 & 14-15 for just an upstream (.diff) patch. Use steps 1-4,5-7,11-13 & 15 for just an Ubuntu (.debdiff) patch.
  1. Open a terminal window
  2. Create a working directory with the command mkdir working
  3. Move to the working directory with the command cd working
  4. Download the latest source package using the command apt-get source packagename. This method automatically appends an '.orig' suffix, and unpacks the file, too. DON'T download the source from packages.ubuntu.com; instead ADD the repos to your Software Sources control panel using the instructions at the Ubuntu Wiki
  5. Make a copy of the unpacked folder with cp -r package-folder package-folder-orig
  6. Go into the unpacked folder (not the orig) with cd package-folder.
  7. Edit the file using nano path/to/file/to/fix. Fix the file
  8. Return to the working directory with cd ..
  9. Use diff -Nurp package-folder-orig package-folder > upstream-bug#.diff to create the upstream patch.
  10. Go back into the unpacked folder (not the orig) with cd package-folder.
  11. Use the command dch -i to update the changelog. Show the change and list the bug# fixed.
  12. Use debuild -S -us -uc to create the debdiff patch.
  13. Attach the .debdiff patch to the bug in Launchpad.
  14. Attach the upstream .diff patch to the Launchpad bug AND the upstream bug.
  15. Delete the working directory, and all contents.
Update February 5, 2009: Accepted and fixed!

Saturday, September 20, 2008

Cron to be deprecated in favor of Upstart in Ubuntu

Ubuntu's Upstart is an init daemon replacement, quite analagous to OS X's launchd. Launchd also replaced cron on OS X - and upstart plans to replace cron on Ubuntu. No telling when, but all my cron jobs will need to be reformatted.

Update: Sept 2011. Three years later and still waiting...