Friday, March 8, 2013

Deciphering mysql error codes

I was tasked to upgrade the mysql server running on the Centos server.

Of course, the first thing I should do was to backup my database. I run the mysqldump command below to write out the sql statements that will re-create and populate the database tables.

# mysqldump -uroot -p -l --opt --all-databases=true > /root/mysqlall.sql
mysqldump: Couldn't execute 'show fields from `asset`': Got error 28 from storage engine (1030)

Lo and behold, I got an "error 28". What is "error 28"? The error message has no useful details that could help me troubleshoot.

The perror command came in handy in that situation.

# perror 28
OS error code  28:  No space left on device

Now, a quick df command confirmed that the hard-drive was full.

perror explains system error codes generated by mysql and the base OS(Linux).

Saturday, March 2, 2013

Auto fill in ssh client parameters

I often ssh to different servers, both at work and at home. Often, the Linux account name is different according to which server I'm logging in. Also, some servers are set up to allow ssh login through a different port than the default port 22. For instance, to login to 123.123.123.123, I need to type all this in:

# ssh -p 2222 admin_2@123.123.123.123

All of this can become human unmanageable rather quickly.

Luckily, I can set up the ssh client such that it fills in ssh login parameters such as port number and user name.

I simply type ssh followed by the IP address or hostname of the computer I want to login. ssh fills in the right port # and user name according to the IP or hostname.

# ssh 123.123.123.123
admin_2@123.123.123.123's password: 

To set it up, insert the following lines into your personal ssh client configuration file, ~/.ssh/config.

Host 123.123.123.123
   User admin_2
   Port 2222

Note that you should edit the configuration file on the ssh client computer. In other words, the setup is on the source computer from which you initiate the ssh request, not the target server that you want to login to.

Alternatively, you can make the change for all users. Insert the same lines into the system-wide ssh client configuration file. For Debian-based distributions, the file is /etc/ssh/ssh_config. You will need root access to edit that file.

After you make the edits, any new ssh client login will have the port and user name filled in automatically.

In addition to ssh, programs such as scp and sftp will also benefit from these settings.

# scp afile.txt  123.123.123.123:
admin_2@123.123.123.123's password: 
afile.txt                                  100%  198     0.2KB/s   00:00    

If you like this article, you may find this article interesting:

How to disable ssh host key checking

Saturday, February 23, 2013

Splitting up is easy for a PDF file

Occasionally, I needed to extract some pages from a multi-page pdf document. Suppose you have a 6-page pdf document named myoldfile.pdf. You want to extract into a new pdf file mynewfile.pdf containing only pages 1 and 2, 4 and 5 from myoldfile.pdf.

I did exactly that using pdktk, a command-line tool.

If pdftk is not already installed, install it like this on a Debian or Ubuntu-based computer.

$ sudo apt-get update
$ sudo apt-get install pdftk

Then, to make a new pdf with just pages 1, 2, 4, and 5 from the old pdf, do this:

$ pdftk myoldfile.pdf cat 1 2 4 5 output mynewfile.pdf

Note that cat and output are special pdftk keywords. cat specifies the operation to perform on the input file. output signals that what follows is the name of the output pdf file.

You can specify page ranges like this:

$ pdftk myoldfile.pdf cat 1-2 4-5 output mynewfile.pdf

pdftk has a few more tricks in its back pocket. For example, you can specify a burst operation to split each page in the input file into a separate output file.

$ pdftk myoldfile.pdf burst 

By default, the output files are named pg_0001.pdf, pg_0002.pdf, etc.

pdftk is also capable of merging multiple pdf files into one pdf.

$ pdftk pg_0001.pdf pg_0002.pdf pg_0004.pdf pg_0005.pdf output mynewfile.pdf 

That would merge the files corresponding to the first, second, fourth and fifth pages into a single output pdf.

If you know of another easy way to split up pages from a pdf file, please tell us in a comment. Much appreciated.

Two updates (part 2, part 3) are available for this post.

Saturday, July 21, 2012

Convert units of measurement

How do you convert from 1 measurement unit to another unit? Say from kilometers to miles.

I can always look it up on that fridge magnet. This one local realtor likes to send me freebies like the fridge magnet with the common conversion factors.

If you don't have a handy fridge magnet, you can try the units command-line utility program.

To install it on a Debian system, enter:

# apt-get install units

# units 60km miles
 * 37.282272
 / 0.0268224

It tells you that 60 kilometers is equivalent to 37.282272 miles.

The second line (beginning with the '/') is somewhat confusing. It is saying, 60 km = 1 / 0.0268224 miles.

Running units in verbose mode makes it clearer.

# units -v 60km miles
 60km = 37.282272 miles
 60km = (1 / 0.0268224) miles

If you like to put spaces in your search parameters, remember to enclose it with quotes.

# units '60 km'  miles
 * 37.282272
 / 0.0268224

If you just want to know the conversion factors, do this:

# units km miles
 * 0.62137119
 / 1.609344

Very quickly, you know:
1 km = 0.62137119 mile, and
1 mile = 1.609344 km.

To many people, installing units and learning all that syntax may be a little too much work.

You can google the answer.

Simply bring up your web browser and go to the Google search page.

Enter this to search:
60 km in miles

The top item returned is the answer you are looking for.

Google Everything.

Sunday, April 11, 2010

How to insert a file at a specific line and column

My objective is to insert the complete contents of a text file at a specific row and column of another text file.

If we are merely concerned with inserting after a specific line, it can be readily achieved with a number of Linux tools. For example, to insert file1.txt after the second line of file2.txt, any of the following commands will do:

  • $ sed -i '2r file1.txt' file2.txt

  • $ awk '{print} NR==2 {while (getline < "file1.txt") print}' file2.txt

    Unlike the previous sed command which modifies file2 in-line, the above awk command writes the desired output to standard output only.

  • $ emacs -batch +3 file2.txt --insert file1.txt -f save-buffer -kill

    This uses the batch capability of the emacs text editor. The command opens file2 at line 3, inserts file1, saves and then exits.

  • $ vi +2 file2.txt << DELIM
    > :r file1.txt
    > :wq
    > DELIM
    Vim: Warning: Input is not from a terminal

    Note that after you enter the first line ("vi +2 ..."), you will be prompted for more input. At that time, you enter the next three lines (:r, :wq, DELIM)

While Linux has many utilities to manipulate text (sed, perl, awk, cut, python), the easiest way I can think of to accomplish my objective to insert at a target line and column is using emacs.

$ emacs -batch -Q +2:3 file2.txt --insert file1.txt -f save-buffer -kill 2>/dev/null

The key is +2:3 which directs the editor to open the file at line 2 column 3.

Two other components in the above emacs command warrant some explanation. First, -Q means quick startup. Quick, because emacs won't load any init file, or any splash file. Second, the last part of the command pipes any standard error output from the emacs editor to the null device.

I don't doubt that sed, awk or perl can do the job. If you have a simple solution, please share with us via the comment feature of this web page. Many thanks.