Sunday, April 06, 2008

I/O Redirection in shell or shell scripting

You will come across this situation when you want to run a process in background. Mostly, you want to capture the output or the errors thrown by the process.

As you must be already knowing, three file descriptors are available by default in any programming language. Similar is the case for a shell or shell scripting. The three are standard input (stdin), standard output (stdout) and standard error (stderr). Their corresponding file descriptors are 0, 1 and 2.

You can use these file descriptors while redirecting your input/output.

Syntax for redirection is as follows
M>N
# "M" is a file descriptor, which defaults to 1, if not explicitly set.
# "N" is a filename.
# File descriptor "M" is redirect to file "N."


Example: To redirect stdout to filename
1>filename
To redirect stderr to errorlog
2>errorlog

Another format is
M>&N
# "M" is a file descriptor, which defaults to 1, if not set.
# "N" is another file descriptor.


Example: To redirect both stderr and stdout to the same file
1>somefile 2>&1

More about I/O redirection here.

MySQL: Purge Master Logs

Binary logs sometime take up too much space on the server. I wanted space so I had to delete the database logs. They were taking up around 2GB space.

http://dev.mysql.com/doc/refman/5.0/en/purge-master-logs.html --> this link will tell you how to delete the logs.

If you want to turn off the logs, then edit /var/mysql/my.cnf on Ubuntu and comment out the following lines
  • line starting with log_bin
  • line starting with expire_log_days
  • line starting with max_binlog_size
If you want to save space and also want the logs then you can ignore commenting log_bin line and try reducing the values for expire_log_days and max_binlog_size.

expire_log_days will tell you the number of days for which the logs will be kept. If it's value is 2. Then all logs older than 2 days will be deleted.

max_binlog_size is the maximum size of the log file. If a log file reaches this limit, then a new log file will be created and appended to. Note, the old log file will not be deleted unless expire_log_days condition satisfies.

Tuesday, April 01, 2008

Move running process to background in Linux

Use the following sequence of keystrokes if you want to move a running process to background in Linux.

CTRL+Z

bg


CTRL+Z will suspend the process and the command prompt will return to you. Type bg and press enter to send the process to background.

If you want to logout but keep the process running even after logging out then type disown -a. This will detach the process and it will continue to run even if you log out.

To start a process in detached mode, use nohup command.

nohup <command to start the process>

man nohup for more details.