Tuesday, 13 November 2012

Some Useful Linux Commands

1. Grep more than one word simultaneously
grep -E 'WARNING|CRITICAL' /usr/local/groundwork/nagios/var/nagios.log
[1352764800] CURRENT SERVICE STATE: HOST1;Memory_Usage;CRITICAL;HARD;4;Memory CRITICAL - 97.0% (4020272 kB) used [1352769686]SERVICE ALERT: HOST2;PING_Win;WARNING;SOFT;1;PING WARNING - Packet loss = 28%, RTA = 4.86 ms

2. To extract RPM contents into current folder
rpm2cpio RPM-file-name.rpm | cpio –idmv

3. To boot Linux system in Single user mode to change root password Type ‘a’ on GRUB boot menu and type single and hit Enter to continue to boot into Single user mode.

4. To extend Xen VM disk size
Shutdown VM
Append 1GB diskspace to existing disk using below command
# dd if=/dev/zero bs=1G count=1 >> disk_file.img
Boot VM
create LVM type partition on newly added space using fdisk
Initialize Physical Volume on this partition, and add it to existing Logical Volume

5. To monitor nagios (or any) process and its child activity
Find parent nagios process pid here it is 25739
ps -aef | grep nagios.cfg
nagios   25739     1  0 12:37 ?        00:00:00 /usr/local/groundwork/nagios/bin/nagios -d /usr/local/groundwork/nagios/etc/nagios.cfg
root     26458 25761  0 12:38 pts/1    00:00:00 grep nagios.cfg

Now list processes starting from that PID
pstree -apAl 25739
nagios,25739 -d /usr/local/groundwork/nagios/etc/nagios.cfg
  |-{nagios},25740
  |-{nagios},25741
  `-{nagios},25742

Use watch command to monitor the tree continuously
watch -n 1 'pstree -apAl 25739'

6. To create 10GB virtual disk by name xyz.img 
dd if=/dev/zero bs=1G count=10 > xyz.img

7. To share a folder (/mydisk) using NFS to all hosts and users of remote host 
Make below entry in /etc/exports file of source system
/mydisk *(rw,sync,no_root_squash)
From remote system mount mydisk by using mount command as shown below
mount source IP:/mydisk /mnt

8. To retain exit value after pipe
false| true ; exit ${PIPESTATUS[0]}
In above example false is returned as exit value.

9. To get summarized disk usage inside the folder
du -hs *
20G     mysql
1.1G    php
2.9G    pnp

10. To print epoch time in date format
date -d @1406063134
Wed Jul 23 02:35:34 IST 2014

11. To print current time in epoch format
date +%s
1406104151

No comments:

Post a Comment