Search This Blog

Tuesday, February 10, 2009

How To Tail (View) Multiple Files on UNIX / Linux Console

tail is one of the best tool to view log files in a real time (tail -f /path/to/log.file). The program MultiTail lets you view one or multiple files like the original tail program. The difference is that it creates multiple windows on your console (with ncurses). This is one of those dream come true program for UNIX sys admin job. You can browse through several log files at once and do various operations like search for errors and much more.

Install MultiTail

Type the following command under Debian / Ubuntu Linux: $ sudo apt-get update $ sudo apt-get install multitail If you are using FreeBSD, enter: # portsnap fetch update # cd /usr/ports/sysutils/multitail # make install clean

How To View Multiple Files Like tail Command

To view /var/log/messages and /var/log/auth.log, enter: # multilog /var/log/messages /var/log/auth.log Sample output:

How do I run a command and view a log file?

Simply use command as follows: # multitail /var/log/iptables.log -l "ping server.nixcraft.in" OR # multitail /var/log/httpd.log -l "netstat -nat" The -l option allows command to execute in a window. Do not forget to use "'s if the external command needs parameter! (e.g. -l "ping host").

How do I display 3 logfiles in 2 columns?

To see all 3 files related to anti mail server gateway, enter: # multitail -s 2 /var/log/maillog /var/log/FuzzyOcr.log /var/log/antivirus.log multitail has many other useful options. Please read man page for further details: man multitail

Regular Expression

Regular Expression is really cool, All should know it;) Initially you might feel that its too complex but when u get into it u feel its look complex but it is really cool and easy to use. It completes big task in few lines....... Its really great;) Below is my first function in Python which i have used to search for a String using '*' to escape and search in string. Cheers Python B) def SearchString(pattern,string): ulist = pattern.split('*') if len(ulist) > 1: k = '^' + ulist[0] + '[\w|\_|\-]*' + ''.join([ i + '[\w|\_|\-]*' for i in ulist[1:-1]]) + ulist[-1] + '$' print k if re.search(k,user) != None: return "Found" else: return "Not Found" elif ulist == string: return "Found" else: return "Not Found" >>> SearchUser('vin*jda*is','vinojdavis') ^vin[\w|\_|\-]*jda[\w|\_|\-]*is$ 'Found' >>> SearchUser('vi**jda*is','vinojdavis') ^vi[\w|\_|\-]*[\w|\_|\-]*jda[\w|\_|\-]*is$ 'Found' >>> SearchUser('vin*jda*is','viojdavis') ^vin[\w|\_|\-]*jda[\w|\_|\-]*is$ 'Not Found'

Tuesday, February 3, 2009

Kill process in Linux or terminate a process in UNIX or Linux systems

Q. How do I kill process in Linux?

A. Linux and all other UNIX like oses comes with kill command. The command kill sends the specified signal (such as kill process) to the specified process or process group. If no signal is specified, the TERM signal is sent.

Kill process using kill command under Linux/UNIX

kill command works under both Linux and UNIX/BSD like operating systems.

Step #1: First, you need to find out process PID (process id)

Use ps command or pidof command to find out process ID (PID). Syntax: ps aux | grep processname pidof processname

For example if process name is lighttpd, you can use any one of the following command to obtain process ID: # ps aux | grep lighttpdOutput:

lighttpd  3486  0.0  0.1   4248  1432 ?        S    Jul31   0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
lighttpd  3492  0.0  0.5  13752  3936 ?        Ss   Jul31   0:00 /usr/bin/php5-cg

OR use pidof command which is use to find the process ID of a running program: # pidof lighttpdOutput:

3486

Step #2: kill process using PID (process id)

Above command tell you PID (3486) of lighttpd process. Now kill process using this PID: # kill 3486 OR # kill -9 3486 Where,

  • -9 is special Kill signal, which will kill the process.

killall command examples

DO NOT USE killall command on UNIX system (Linux only command). You can also use killall command. The killall command kill processes by name (no need to find PID): # killall -9 lighttpd Kill Firefox process: # killall -9 firefox-bin As I said earlier killall on UNIX system does something else. It kills all process and not just specific process. Do not use killall on UNIX system (use kill -9).

Sunday, February 1, 2009

Variable Datatypes & Range in C

Variable Types:
There are a number of ‘built-in’ data types in C. These are listed below. Where a
shorter version of the type name exists, this is given in brackets; essentially the base
type int is implicit whenever short , long, or unsigned are used.
  1. short int (short)
  2. unsigned short int (unsigned short )
  3. char
  4. unsigned char
  5. signed char
  6. int
  7. unsigned int (unsigned )
  8. long int (long)
  9. unsigned long int (unsigned long )
  10. float
  11. double
  12. long double
The range of values that can be stored in variables of these types will depend on the
compiler and computer that you are using, but on an IBM PCs and the Borland Turbo
C compiler the ranges are:
  • short int -128 ® 127 (1 byte)
  • unsigned short int 0 ® 255 (1 byte)
  • char 0 ® 255 or -128 ® +127 2 (1 byte)
  • unsigned char 0 ® 255 (1 byte)
  • signed char -128 ® 127 (1 byte)
  • int -32,768 ® +32,767 (2 bytes)
  • unsigned int 0 ® +65,535 (2 bytes)
  • long int -2,147,483,648 ® +2,147,483,647 (4 bytes)
  • unsigned long int 0 ® 4,294,967,295 (4 bytes)
  • float single precision floating point (4 bytes)
  • double double precision floating point (8 bytes)
  • long doubl e extended precision floating point (10 bytes)