Monday, October 22, 2007

Redirect stderr to both screen and file at the same time

I recently ran into a situation where I need to run a command and have the error output recorded in a file while displaying it on the screen at the same time. It turns out the 'tee' command comes in handy in this case.
$ command 2>&1 | tee err.out

To redirect stdout and stderr to different files, and have the stderr goes to the screen as well:
$ command 2>&1 > std.out | tee err.out

To redirect stdout and stderr to the same file while having the stderr displayed on the screen, too:
$ command 2>&1 > all.out | tee -a all.out


The last trick is particularly handy when it comes to writing a cron job. The crontab will only send out email when there is stderr outputs while writing the output to the same file as the stdout.

Tuesday, September 11, 2007

How to Make Sure MySQL is Running?

To ping MySQL,
$ mysqladmin -u root ping
mysqld is alive

To check MySQL Server's version,
$ mysqladmin -u root version
mysqladmin  Ver 8.41 Distrib 5.0.77, for redhat-linux-gnu on x86_64
Copyright (C) 2000-2006 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version          5.0.77
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 22 min 25 sec

Threads: 1  Questions: 6  Slow queries: 0  Opens: 12  Flush tables: 1  Open tables: 6  Queries per second avg: 0.004

Wednesday, August 1, 2007

Standard Input and Output in Linux

Every process in Linux has 3 connections to the outside world.  They are:

  • Standard Input (stdin): default to the keyboard
  • Standard Output (stdout): default to the monitor screen
  • Standard Error (stderr): default to the monitor's screen
In Unix/Linux each of these connections is also associated with a file descriptor:
  • 0: stdin
  • 1: stdout
  • 2: stderr
To redirect a process's output to a file (only normal messages, no errors):
Standard input can also be represented by '-'.
This is equivalent to copy con outputfile in DOS.
$ command > outputfile 
$ command - > outputfile

To direct a file as standard input: 
command < inputfile

To direct standard input to a file:
cat  > outputfile

To direct a process's output to another process (command) as standard input, use pipe:
command_out | command_in
e.g. cat filename | more

To direct error to a file:
   $ command 2>errors.txt  # 2 indicates the error stream in Linux
   $ command 2>/dev/null   # /dev/null is a device where anything you send simply disappears.

To direct both standard output and error to a file:
   $ command > error.log 2>&1  # '&1' represent stdout.
To direct stdout to output.log and stderr to error.log, and print only error to the screen:
   $ command 2>&1 > output.log | tee error.log

   # append error to the same output.log file instead of error.log.
   $ command 2>&1 > output.log | tee -a output.log