Useful Linux Trick: cron @reboot

words by Brian Racer

There are various ways to make sure something is run at system startup – Redhat has /etc/rc.local script, and it and many others have /etc/init.d/* scripts – but many times you might not have access to those files or creating init scripts might be overkill for your needs.

People are always amazed when I tell them they can achieve this basic functionality by using cron. Many of our websites use Sphinx, the excellent full text indexer, to allow site searches. Should the server ever reboot, we need to make multiple search daemons start back up. Take the following line from a crontab:

crontab -l
@reboot /usr/local/bin/searchd --config ~/conf/sphinx.conf

This will make sure the searchd daemon starts on bootup.

Also there are a few other shortcuts you can use:

@yearly        Run once a year, "0 0 1 1 *".
@annually      (same as @yearly)
@monthly       Run once a month, "0 0 1 * *".
@weekly        Run once a week, "0 0 * * 0".
@daily         Run once a day, "0 0 * * *".
@midnight      (same as @daily)
@hourly        Run once an hour, "0 * * * *".

See man 5 cron for more information.

(And to be pedantic, @reboot is run when cron is started or restarted, not necessarily the OS itself. So /etc/init.d/cron restart would trigger that line to be run. You may want to keep that in mind.)

chkdsk and Grub

words by Brian Racer

Recently I used GParted to resize an NTFS disk to dual boot Ubuntu and Windows XP. I finished the Ubuntu installation and everything seemed to be working fine until I tried to boot back into XP. Windows reported there might be disk corruption, ran chkdsk, and chkdsk ended up freezing. I rebooted again and saw that the grub bootloader was now also freezing. Delightful. Although I didn’t think chkdsk modified the MBR, upon further research in some cases it does(when run with the /r switch) and in this case ends up corrupting the MBR. To fix this issue you can perform the following:

Boot with an Ubuntu Live CD and open up a terminal.

sudo grub
grub> find /boot/grub/stage1

Note the hdx number and partition number to it’s right. Now type the following commands into the grub prompt:

grub> root (hdx, y)
grub> setup (hdx)
grub> quit

Note whitespace is important here. Now you can reboot. When Windows asks to run chkdsk hit a key to cancel. Once in Windows, open a command prompt and type:

chkntfs /c c:

This will schedule a disk check on reboot that will not alter the MBR. Reboot and allow the disk check to complete. It may automatically reboot your system again, but once that is finished both OS’s should boot fine from now on.

SVN autobackup restore

words by Brian Racer

I have been using Doug Hellman’s useful svnautobackup script for our subversion backups. I think it is a pretty useful script, but it only focuses on backups and not restoration.

During a recent server upgrade I needed to restore a large amount of repositories from our dumpfiles. I made the following shell script in php that will loop through all of the backup dumps and restore them. There is a similar tool that uses ruby, but it can only restore one repository and I do not know enough ruby to modify it 🙂

#!/usr/bin/php -q
// edit this
$repo_dir = '/svn/';
$backup_dir = '/home/backup/svn/';
// don't edit this
foreach(new DirectoryIterator($backup_dir) as $file)
  if(isDir() && !$file->isDot())
    // get files
    $dumpfiles = glob($backup_dir.$file.'/dumpfile*.bzip2');
    // sort them
    // create the repo if not exists
      $cmd =  "svnadmin create {$repo_dir}{$file}";
    // restore dump files
    foreach($dumpfiles as $dumpfile)
      $cmd = "bzcat {$dumpfile} | svnadmin load {$repo_dir}{$file}";

For reference, here is the script that runs nightly though cron

files=`ls /svn`
for file in $files
  if [ -d /svn/$file ]
    mkdir -p $file
    /home/backup/scripts/svnautobackup/ -v -i 100 --history-file /home/backup/svn/$file-hist --out-dir /home/backup/svn/$file /svn/$file