Archive for the 'General Tips' Category

October 4th 2007

Page mySQL query results through your favorite pager

Every time I connect to a mysql server, I see the “Type 'help;' or '\h' for help. Type '\c' to clear the buffer.” notice but just ignore it. This morning I decided to ‘\h’ just to see what the in-line help was and found a score of useful commands I had never known about before, possibly the most useful one being ‘\P’.

pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.

if I had a nickel for every minute I spent changing the scrollback of my terminal and then scrolling back for pages and pages to investigate query results I’d be a thousandaire. A simple ‘\P less;‘ now makes my life a little easier. Of course, if you don’t think less is more, you can ‘\P more;‘ instead.

For the curious, here’s the totality of the ‘\h’ output:

List of all MySQL commands:
Note that all text commands must be first on line and end with ‘;’
? (\?) Synonym for `help’.
clear (\c) Clear command.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don’t write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don’t show warnings after every statement.

No Comments yet »

September 15th 2007

Being nice With Long Processes

Every once and a while, I find that I need to compile code on a machine where many other more important things are running. In my case, I generally find myself recompiling PHP on a machine that also runs my very active forum website [http://forums.reebosak.net] in addition to many other websites, including this one. Running a long build is not exactly the nicest thing to do to users who are trying to go about their normal browsing, where a long build would cause really long page loads since the processor is so busy. The way to deal with this is by being nice (and renice).

from the UNIX man page:

NAME

       nice - run a program with modified scheduling priority

DESCRIPTION

       Run  COMMAND  with an adjusted niceness, which affects process schedul-
       ing.  With no COMMAND, print the current  niceness.   Nicenesses  range
       from -20 (most favorable scheduling) to 19 (least favorable).

To set a processes niceness run it with the nice command:

[brien@toothbrush ~]$ nice -n 5 make // run make with +5 niceness (lower priority)
[brien@toothbrush ~]$ ps aux |grep make
brien      4560  0.0  0.1  5040 1180 pts/1    SN+  18:36  0:00 make
[brien@toothbrush ~]$ sudo renice -3 4560 // change the niceness of the process to -3 (higher priority)

No Comments yet »

May 4th 2007

Easily Syncing Remote Files in Komodo With Unison

In my day to day work, I edit code on one machine, then move the new code to a different machine to build packages for installation and testing. Until recently I was doing one of two things:
1) Mounting an SMB/NFS share and editing the remote files directly
2) Using rsync to copy files back and forth from my dev machine to my build machine

Both methods “worked” but had their downsides. With the remote SMB, I had to have network access if I wanted to work. Since I was also directly editing the source, I only had one copy and if I hadn’t checked in to CVS yet, and something went wrong, I could be screwed. I couldn’t work on anything if I was not connected to the corporate network here at work (ether physically, or via ssh tunnel).
rsync worked alright, but the syncing is one way, so I had to do some weird double syncing to make sure all the changes were propagated. I had to be really careful with edits because if two files were changed in both locations, I would again screwed - no merging. One side of the changes would always be lost. The other downside to this was that if the system times on each machine were not *exactly* the same, rsync would sync files that were not edited, which changed the file’s timestamp, which then caused CVS to see them as edited. It was a mess.

A couple of weeks ago I found Unison:

Unison is a file-synchronization tool for Unix and Windows. It allows two replicas of a collection of files and directories to be stored on different hosts (or different disks on the same host), modified separately, and then brought up to date by propagating the changes in each replica to the other.

With Unison, you simply set up the source and destination directories, which can be local or remote and then tell it to sync. There are optional ways of connecting to remote directories, I personally use ssh, which then uses an ssh tunnel I have set up in my ssh_config file.

This worked great, but I soon grew a little tired of editing files in Komodo, flipping to my Unison window, syncing, then flipping back to Komodo. Then it hit me, “why don’t i just set this sync up as a toolbox item?”. Unison has a command line interface, so after a quick read of the ‘-help’ output, I created a toolbox item with unison -batch -auto; (read the ‘-help’ to customize your sync with your own preferences), then set up a keyboard shortcut CTRL+ALT+S to trigger it. Now I can happily edit my code on my laptop and sync with a quick flick of the fingers.

NOTE: I have this set up in Linux, but since both Komodo and Unison are available for Windows, it should be trivial to accomplish the same in that environment

No Comments yet »