How do I rotate log files?

last updated in Categories , , , ,

Q. How do I rotate log files under Linux operating system?

A. You need use tool called logrotate, which is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files.

Each log file may be handled daily, weekly, monthly, or when it grows too large. With this tool you keep logs longer with less disk space.

Default configuration file

The default configuration file is /etc/logrotate.conf:
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}
Service or server specific configurations stored in /etc/logrotate.d directory, for example here is sample apache logrotate configuration file:# cat /etc/logrotate.d/httpdOutput:

/var/log/httpd/*.log {
 weekly
 rotate 52
 compress
  missingok
  notifempty
  sharedscripts
  postrotate
      /bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true    endscript
}

Where,

  • weekly : Log files are rotated if the current weekday is less then the weekday of the last rotation or if more then a week has passed since the last rotation.
  • rotate 52 : Log files are rotated 52 times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather then rotated.
  • compress : Old versions of log files are compressed with gzip to save disk space.
  • missingok : If the log file is missing, go on to the next one without issuing an error message.
  • notifempty : Do not rotate the log if it is empty
  • sharedscripts : Normally, prerotate and postrotate scripts are run for each log which is rotated, meaning that a single script may be run multiple times for log file entries which match multiple files. If sharedscript is specified, the scripts are only run once, no matter how many logs match the wildcarded pattern. However, if none of the logs in the pattern require rotating, the scripts will not be run at all.
  • postrotate
    /bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
    endscript : The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. These directives may only appear inside a log file definition.

See complete working example: How to rotating logs with logrotate for Lighttpd web server.

How do I use cpio command under Linux?

last updated in Categories , , , , , , , ,

Q. Can you explain how do I use cpio under Linux / UNIX?

A. GNU cpio is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files. cpio command works just like tar, only better.

As I said earlier, cpio works like tar but it can read input from the “find” command. This is nifty feature. For example you can find out all *.c files and backup with cpio command.# find / -name "*.c" | cpio -o --format=tar > c-file.backup.tar
# find / -iname "*.pl" | cpio -o -H tar > perl-files.tar
You can also specify file name using -F option:# find / -iname "*.pl" | cpio -o -H tar -F perl-files.tarWhere,

  • -o: Create archive
  • -F: Archive filename to use instead of standard input or output. To use a tape drive on another machine as the archive.
  • -H format: Specify file format to use.
  • -i: Restore archive

You can extract archive with the following command:# cpio -i -F perl-files.tarYou can list file inside archive i.e. list contents of the cpio file with following command:# cpio -it -F perl-files.tarYou can write archive of /home to tape (drive /dev/nst0), type the following command:# find /home | cpio -o -H tar -F /dev/nst0Restore backup using following command:# cpio -i -F /dev/nst0Backup /home dir, to remote system tape drive:# find /home | cpio -o -H tar -F user@backup.nixcraft.in:/dev/nst0 --rsh-command=/usr/bin/ssh

See what is going on the Linux console screen

last updated in Categories

/dev/vcsN is a character device i.e. it refers to the memory of the currently displayed virtual console terminal. These are character devices for virtual console terminals, they have major number 7 and minor number 1 to 63.

You may do a screendump on using cat command (vt2) or just see what is going on the Linux console screen:

# cat /dev/vcs2 > /tmp/output.vt2.
# cat /dev/vcs2

It may be handy to monitor remote system vts.

How do I install a Perl Module?

last updated in Categories , , , , , , , , ,

Q. How do I install a Perl module under Linux or UNIX like operating system?

A. Perl modules (a Perl module is a discrete component of software for the Perl programming language) can be installed using various methods under different Linux distributions.

Task: Install perl module

If you are using Red Hat Linux then use up2date command to install most common modules:

# up2date module-name
Use following command to list all supported modules:
# up2date --showall | grep -i 'perl'
OR use yum command under CentOS/RHEL 5/Fedora core:
# yum list available 'perl'

If you are using Debian Linux, try to use apt-get command to install common perl module. However following is the recommended way to install a perl module.

A) Start CPAN Shell:
# perl -MCPAN -e shell

B) Install a perl module:
At cpan> shell prompt install module using install module::Name command. For example install module called MIME::Lite:
# cpan> install MIME::Lite
Alternatively, try out the following command:
# cpan -i MIME::Lite

See also:

MySQL startup script under BSD/Linux

last updated in Categories

You can start the MySQL server directly as described here. But most Linux distribution comes to special start and stop script. First, you must login as the root user. Use su – command to login as the root user.

If you are using Red Hat Linux / Fedora Linux:
Start MySQL

# service mysql start

OR

# /etc/init.d/mysql start

Stop MySQL

# service mysql stop

OR

# /etc/init.d/mysql stop

If you are using Debian Linux:
Start MySQL

# /etc/init.d/mysql start

Stop MySQL

# /etc/init.d/mysql stop

If you are using FreeBSD:
Open /etc/rc.conf file and add following line:

vi /etc/rc.conf

And append following line:

mysql_enable=”YES”

To Start MySQL under FreeBSD:

#/usr/local/etc/rc.d/mysql-server.sh start

To Stop MySQL under FreeBSD:

#/usr/local/etc/rc.d/mysql-server.sh stop