X-Combinator

Avatar

making the human scalable

Repairing your MySQL database

Today one of our computer’s /usr partition filled up and caused data corruption in the database. After space was freed up on the partition, we (Moises and I) ran some commands to repair the database. Here is what we did:

  1. cd into your directory where your db files are stored. In our case it was in the /usr…/data/mysql/ folder.
  2. We then run the command:
    myisamchk *.MYI | grep -3 –color corrupted
  3. This should give you some output on the current state of your files and indicate which files are corrupted and needing repair. It also gives you the next command to run on the line that reads:
    Fix it using switch “-r” or “-o”
  4. So that is just what we did
    myisamchk -r file.MYI
  5. Voila, your db should be repaired

Backspace in Screen

I finally got sick of hitting Ctrl-H to backspace while in a screen session today, so I found a way around it:

Try editing

~/.bashrc

And adding:

alias screen='TERM=screen screen'

Not sure if this has adverse effects and there might be a better way to do it, but hey, it works!

another reason to love vim

In vim 7.0 try the following:

:tabnew http://www.google.com
:e # to reload

Very handy to quickly view source, or edit any remote HTML/CSS, etc.

zero-width negative look-ahead assertion

Currently I need to do some bulk transformation of some file names.
I have, say, 3 file names: 2.640-849.0.jpg, g2650ohr.jpg, and k2.26mr.jpg (so these are treated as three separate strings)
I want a (hopefully) single regex to substitute all of the . (periods) with - (dashes) except for the .jpg extension.

A first draft would be

s/\./-/g

but that gives you 2-640-849-0-jpg. Note the extension is transformed as well.

I could then do a second regex like s/-jpg/.jpg/ but that feels less than optimal.

The solution is a zero-width negative look-ahead assertion
man perlre for more information on this, but the basic idea is that
you match occurances of the preceeding expression that ate not followed by the zwnla.
For example /foo(?!bar)/ matches any occurrence of foo that isn’t followed by bar.

Combine this with the common perl utility rename and we get the following:

rename -n 's/\.(?!jpg)/-/g' *

Note that Matz has decided not to include the zero-width negative look-ahead assertion to Ruby.

UPDATE
Thanks goes out to Matt Pulver for submitting these improvements to the above regex:

This will handles cases like test.jpg.jpg

s/\.+(?!jpg$)/-/g

If you want to substitute all non-alpha-numeric characters and support other file extensions try:

s/[^0-9a-z](?!\w+$)/-/g

use env to test scripts for cron and monit

I’ve used /usr/bin/env for years but I just realized today that it can be very useful for testing processes that are run with cron or monit.

env has a nice option for clearing out all of the environment variables:

-i, --ignore-environment - start with an empty environment

Now you can try to run your script in an environment very similar to cron or monit. Note that this option even clears the PATH so you need to specify full paths or specify a PATH variable.

/usr/bin/env -i HOME=/path/to/my/home /path/to/do_script.sh

increment the number in a file

Lets say you have a file called REVISION which contains a single number. If you want to increment the number in that file you could run the following command:

Wrap that up as a nice shell script and you get a nice increment command:


[nathan@nate ~]$ cat REVISION
1
[nathan@nate ~]$ increment REVISION
[nathan@nate ~]$ cat REVISION
2

djb daemontools with Ubuntu’s upstart

In case anyone wants to get djb’s daemontools up and running quickly on an OS that uses Ubuntu’s System V init-replacement upstart, here’s what works for me:

Create a 3-line file /etc/event.d/svscanboot :

That should start svscanboot with each system bootup. To have init start svscanboot without rebooting, do

as root after creating the above file.

,