Tuesday, February 12, 2013

How to check if a file is being accessed by any process ?

Use 'lsof' command with the following syntax:
# lsof -f -- <path of file name>

Example:
linuxmach# lsof -f -- /var/log/secure
COMMAND  PID USER   FD   TYPE DEVICE  SIZE   NODE NAME
syslogd 3065 root    3w   REG  253,0 40558 169083 /var/log/secure
linuxmach#
The file '/var/log/secure' is being accessed by "syslogd" dameon with the PID 3065 and it is being written now.

Monday, January 7, 2013

Using RegEx in find command

Below shown are the examples for using Regular Expression in 'find' command:

[ashok@linuxhost workdir]$ find . -type f -regex ".*[0-9]+.txt" -print 2> /dev/null
./1322143.txt
./anything_134.txt
./12345_837.txt
./something10984.txt

[ashok@linuxhost workdir]$ find . -type f -regex ".*/[0-9]+.txt" -print 2> /dev/null
./1322143.txt
[ashok@linuxhost workdir]$

Wednesday, November 28, 2012

Argument list too long error

At times, you might get into a situation where you can't copy, move, remove or even list a huge number of files in a directory. When you attempt it, an error will be thrown saying "Argument list too long". This actually indicates that the OS has exceeded its Argument list limit. To know the Argument list limitation of your OS, run the following command "getconf ARG_MAX". It will show you the maximum number of arguments that can be passed to mv, cp, rm or ls command.

# getconf ARG_MAX
   131072

To get rid of this problem, you must split your mv or cp command using wildcards.

Like instead of using "cp /folder1/* /folder2/ ",  we should use "cp /folder1/a* /folder2/; cp /folder1/b* /folder2/"
(or)
you can use 'for' loop construct as follows:

# for i in {a..z}; do cp /folder1/$i* /folder2/ ; done &
Please note in 'for' loop, for every cp command it executes, it creates a new process id.

Thursday, November 8, 2012

Oracle ASM commands

For my records, some Oracle ASM admin commands :
oracleasm status
oracleasm listdisks
oracleasm start
oracleasm stop
oracleasm querydisk
oracleasm querydisk /dev/sd*
ls -l /dev/oracleasm/disks
oracleasm listdisks | xargs oracleasm querydisk -p
oracleasm scandisks
oracleasm scandisks -p
oracleasm restart
oracleasm enable
oracleasm disable
oracleasm configure
oracleasm createdisk ASMDISKName /dev/sdx
/usr/sbin/asmtool -C -l /dev/oracleasm -n ASMDISKName -s /dev/sdx -a force=yes
oracleasm deletedisk

Sunday, October 28, 2012

Creating complex password

There could be multiple ways to create Complex password using Linux OS. Here are couple of simple ways with sample execution to create complex password of length 8 characters:

linuxhost $ openssl rand -base64 6
NRwijHK9
linuxhost $ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8; echo
7aSNmEot
linuxhost $

Scripted version, where you can define the characters that you require in the password:
http://smart-scripts-for-sysadmins.blogspot.com/2011/03/script-for-generating-complex-password.html

Wednesday, October 17, 2012

Listing history commands using 'fc'

In Linux, the command 'fc' (stands for find command) can be used as an alternative to 'history'. 
This is a Bash built-in command using which we can list only the selected number of last executed commands.

For example, if you wish to list only last 20 commands, the syntax for 'fc' command should be "fc -l -20".
Below shown is a sample execution:

linuxhost # fc -l -20
1081     ls
1082     cd current/
1083     ls
1084     make
1085     make linux
1086     make target
1087     history | grep iozone
1088     iozone -a -i 0 -i 1 -i 2 -r 32k
1089     df -h
1090     ps -fu gpadmin | grep iozone
1091     man iozone
1092     exit
1093     df -h
1094     df -h
1095     cd /gplum/
1096     du -sh .
1097     top
1098     uname -r
1099     free -m
1100     getconf -a | grep -i pagesize
linuxhost #

Tuesday, October 16, 2012

Finding all the binaries in a Linux machine

# find / -xdev -type f -perm +111 -exec file -i '{ }' \; | grep 'x-executable' > list_of_binaries.txt

# awk -F":" '{print $1}' list_of_binaries.txt > List_of_Binaries.txt

Saturday, October 13, 2012

Comparing directories between 2 Linux systems

This is a continuation of my previous post on comparing directories using 'diff' command that are Local.
However on several occasions I faced situations, where I need to compare the file configuration between 2 Linux machines (especially in Application servers). In those scenario what I normally used to do was, copy the entire directory from server to another and run the 'diff' command as shown in the previous post. It was obviously a tedious task. Eventually I found a solution to simplify this task by using 'sshfs', which is Secure SHell FileSystem (refer 'man sshfs' for more details).

Steps to compare Folders in 2 machines. Let's assume the local folder name "/dir1" and the remote folder name is "remotehost:/dir2":

1. Login to first machine as 'root' user.

2. Create a local mount point to mount the remote folder.
    # mkdir /rmtmnt

3. Mount the Folder in remote machine using 'sshfs'
    # sshfs user@remotehost:/dir2  /rmtmnt

4. Run the 'diff' command
     # diff /dirl /rmtmnt -r --brief

5. After the execution, umount the remote filesystem using 'fusermount -u'.
     # fusermount -u /rmtmnt
    
Please note 'umount' command wouldn't work with sshfs.

Tuesday, September 25, 2012

Comparing directories using 'diff' command

'diff' command can be used to compare not just Files but also Directories/Folders as well.
Following is a self-explanatory demonstration on how the 'diff' command can used to compare or find differences between 2 folders (folder1 & folder2).

[root@hostxyz folder1]# ls -l
total 16
-rw-r--r-- 1 root root 3 Sep 24 12:29 oraclelinux
-rw-r--r-- 1 root root 3 Sep 24 12:28 redhat
-rw-r--r-- 1 root root 3 Sep 24 12:28 suse
-rw-r--r-- 1 root root 3 Sep 24 12:29 ubuntu
[root@hostxyz folder1]# for i in `ls`; do echo $i   : `cat $i`; done      # Contents of all files in folder1
oraclelinux : OS
redhat : OS
suse : OS
ubuntu : OS
[root@hostxyz folder1]# cd ../folder2
[root@hostxyz folder2]# ls -l
total 16
-rw-r--r-- 1 root root 17 Sep 24 12:34 CentOS
-rw-r--r-- 1 root root 17 Sep 24 12:30 oraclelinux
-rw-r--r-- 1 root root  3 Sep 24 12:29 redhat
-rw-r--r-- 1 root root 17 Sep 24 12:30 ubuntu
[root@hostxyz folder2]# for i in `ls`; do echo $i   : `cat $i`; done     # Contents of all files in folder2
CentOS : Operatins system
oraclelinux : Operating system
redhat : OS
ubuntu : Operating system

[root@hostxyz folder2]# diff /var/folder1 /var/folder2 -r --brief     
Only in /var/folder2: CentOS
Files /var/folder1/oraclelinux and /var/folder2/oraclelinux differ
Only in /var/folder1: suse
Files /var/folder1/ubuntu and /var/folder2/ubuntu differ
[root@hostxyz folder2]#

Options used :
 -r  :      Searched recursively through the directories.
--brief : Only shows the names of the files that differ. If you want details of the content that differs, remove this option.

Saturday, August 25, 2012

Finding the device-name associated with a file-system label

We might have came across a situation where a Device associated with a file-system label is missing and due to which we will get an error on boot-up saying file-system can't be found for the Label=[labelname]. To resolve this, we have to find the corresponding Device name associated with the file-system label (set by using the command 'e2label') and fix the problem with it by running 'fsck' or by changing the Label to other alternative disk.
'findfs' is the handy command for it; this is like a reverse case of 'e2label' command.

Below given is a self-explanatory illustration of it:

[root@linuxhost ~]# e2label /dev/sda1
/boot
[root@linuxhost ~]# e2label /dev/sda1 Test
[root@linuxhost ~]# e2label /dev/sda1
Test

[root@linuxhost ~]# findfs LABEL="Test"
/dev/sda1
[root@linuxhost ~]# 

[root@linuxhost ~]# cd /dev/disk/by-label/
[root@linuxhost ~]# ls -l
total 0
lrwxrwxrwx 1 root root 10 Aug 16 08:56 Test -> ../../sda1
[root@linuxhost ~]#

Thursday, August 23, 2012

Total number of 'D' processes

Executing this command-set will display you the total number of uninterruptable sleep (D) processes on the server:

# ps aux | awk '{if ($8 == "D") {print; count++} } END {print "Total No. of uninterruptable Sleep Processes: "count}'

Monday, August 20, 2012

Checking the progress of 'dd' command execution

Start the 'dd' command in back-ground:
[root@hostxyz ~]# dd if=/dev/sda of=/dev/sandisk1 &
[1] <PID>

Execute either of  following while statement against the PID captured in the previous command:
[root@hostxyz ~] # while true; do kill -10 <PID>; sleep 10; clear; done
(or)
root@hostxyz ~] # while true; do kill -USR1 <PID>; sleep 10; clear; done

SAMPLE EXECUTION

[root@hostxyz ~]# dd if=/dev/zero of=/dev/sdf1 &
[1] 2069
[root@hostxyz ~]# while true; do kill -10 2069; sleep 5; clear; done
14207400+0 records in
14207400+0 records out
7274188800 bytes (7.3 GB) copied, 38.1668 s, 191 MB/s
15276232+0 records in
15276232+0 records out
7821430784 bytes (7.8 GB) copied, 43.1825 s, 181 MB/s
17829523+0 records in
17829523+0 records out
9128715776 bytes (9.1 GB) copied, 48.1945 s, 189 MB/s
18739498+0 records in
18739498+0 records out
.
.
<so on>

Sunday, August 19, 2012

Environment variables in Linux

On a Linux system, we can view the Environment variables that are set and exported using 3 built-in commands: 
'set', 'env' & 'export -p'.

Ever wondered how can we see the other 'unset' environment variables belongs to Bash shell or some command or tool ?

The technique that I follow is to look in 2 places: One in the Binaries of the program which the application uses and other in the MAN page.

To view all the printable strings in a Bash shell:  # strings /bin/bash | grep -P '[A-Z]+'
In this output, there will be a lot of additional stuff and you will have to look for environment variables carefully

To print all the ENV variables related to 'history' command:

# man bash | sed 's/[[:cntrl:]].//g' | egrep -x ' +HIST[[:alpha:]]*'
       HISTCMD
       HISTCONTROL
       HISTFILE
       HISTFILESIZE
       HISTIGNORE
       HISTSIZE
       HISTTIMEFORMAT

A URL that contains all the Bash variables explaining each:

Saturday, August 18, 2012

Improving Read performance of Disks using 'blockdev'

The Read performance of a Disk can be improved by increasing a parameter called "Read+Ahead" using 'blockdev' command. By default the Linux OS will read 128 KB of data in advance so that it is already in Memory cache before the program needs it. This value can be increased so as to get better Read Performance.

Steps to check and increase the Read-Ahead value:

To check the current 'blockdev' status of all block device:   # blockdev --report
[root@linuxserver ~]# blockdev --report
RO    RA   SSZ   BSZ   StartSec     Size    Device
rw 16384   512  4096          0   14680064  /dev/sda
rw 16384   512  1024         63     208782  /dev/sda1
rw 16384   512   512     208845   10265535  /dev/sda2
rw 16384   512  4096          0 2147483648  /dev/sdb
rw 16384   512  4096          0   35651584  /dev/sdc
rw 16384   512  2048         63   35648172  /dev/sdc1
rw 16384   512  4096          0  104857600  /dev/sdd

To check the 'Read-Ahead' value of an individual disk (let's take sda)
# blockdev --getra /dev/sda
(or)
# cat /sys/block/sda/queue/read_ahead_kb

To change the 'Read-Ahead' value to 8 MB (16384 times of 512 bytes blocks).
# blockdev --setra 16384 /dev/sda

To make it permanent upon system reboot, just add this command entry in /etc/rc.local.