Tuesday, February 20, 2018

How to run shell commands in parallel under for loop

I came across a situation where i need to run multiple commands at the same time i.e. in parallel.
I had to run the for loop to capture the device name and then run some write tests on all these devices at the same time.

 A "for" loop is completed with "do" , "done" semantic.
 # for disk in `lsscsi |grep 'SDIFC10-0720801'|awk '{print $6}' `; do fio --ioengine=libaio --direct=1 --name=test --filename=$disk --bs=4k --iodepth=10 --size=1000M --readwrite=write ; done

But this will end up running the fio command sequentially one after another.
The little secret is to use "&" instead of ";" after do phrase :
 # for disk in `lsscsi |grep 'SDIFC10-0720801'|awk '{print $6}' `; do fio --ioengine=libaio --direct=1 --name=test --filename=$disk --bs=4k --iodepth=10 --size=1000M --readwrite=write & done

And this will run the fio command on all the disks at the same time.

Saturday, February 4, 2017

Find a line with Control Character

I came into a situation where one of the application was failing to read a file.
There was no clue for that reason and it was just throwing exception of some bad string.
We knew there is something wrong with the file it is trying to read.

So we suspected some control character within the file. In order to find that character we can use the following commands :

Filename: test.txt
Line 1
line 2
line3
linne 4
line 5
line 6

GNU grep :
#/usr/gnu/bin/grep '[^[:print:]]' test.txt
line 5

#/usr/gnu/bin/grep '[[:cntrl:]]' test.txt
line 5

Above example indicates that there is a control character in the above line.

Using "cat" : reveals the control character at line 5 :
#cat -vte test.txt
Line 1$
line 2$
line3$
linne 4$
line 5^M$
line 6$

You can use vi/vim too but if the file is huge it may be difficult.
You can open file in vi and do ":set list" to see control characters

Saturday, March 21, 2015

Print file in reverse using tac

This may not be very useful in everyday life but i just found this command that is available on Solaris as well as other *Nix platforms. If you want to print contents of a file you simply use command "cat" which will print lines from top to bottom. but if you want to print lines in opposite order then jsut use "tac".

Example :
~> cat test.txt
Line 1
line 2
line3
linne 4
line 5

----------
~> tac test.txt
line 5
linne 4
line3
line 2
Line 1
-----------------------------


As you can see in above example the lines are printed in reverse order.

Friday, April 18, 2014

Ubuntu LTS 14.06 Gnome

I was using beta for Ubuntu 14.04 Gnome for sometime on my virtual box. As i don't want to use Default Unity version as it is heavy on my Vbox. Hoping that these issues are fixed in final LTS release but unfortunately this is not the case.

1. But gnome had few issues as after boot it used to crash every time.

2. Sometime after login to gnome it comes up with just black screen and we have to reboot 2 or 3 times hoping that it will come up.
Workaround: Press Ctrl + Alt + F1 terminal will open login and do :
#startx
Otherwise : Enable gnome autologin from /etc/gdm/custom.conf

Automatically gnome session will start and you will get gnome login.

3. Ubuntu Software Center displays software description text which is almost invisible.





Hope these issues will be fixed in updates soon.

If you are interested in final releases for Ubuntu LTS 14.04 then you can download the same here :
Ubuntu Gnome: http://cdimage.ubuntu.com/ubuntu-gnome/releases/14.04/release/
Edubuntu : http://cdimage.ubuntu.com/edubuntu/releases/14.04/release/
Kubuntu: http://cdimage.ubuntu.com/kubuntu/releases/14.04/release/
Lubuntu: http://cdimage.ubuntu.com/lubuntu/releases/14.04/release/
Xubuntu: http://cdimage.ubuntu.com/xubuntu/releases/14.04/release/
Ubuntu Studio: http://cdimage.ubuntu.com/ubuntustudio/releases/14.04/release/

Sunday, January 12, 2014

Dtrace : Time spent in system call or function

Dtrace is a awk and C++ like programming language also called D Language.
It can be used very efficiently to provide amazing results that other tools can't provide.
But Dtrace is not the replacement of other tools but you can say Dtrace is a complement to other tools.
Although it is not the first tool to be used while diagnosing a problem but it can be used at a later point of time to dig much deeper into the particular area of problem.

Let us take an example, if you are suspecting an application to be taking more time than expected then you may want to use first other tools to verify things like physical memory, CPU usage, swap etc. And then later we may want to start looking at the application.

Let us see if we just want to see which system calls or functions in an application are taking most time, then we can use the below simple script :

#!/usr/sbin/dtrace -s
pid$1:::entry
{
    t[probefunc] = timestamp;
}
pid$1:::return
/t[probefunc]/
{
    @funct[probefunc] = sum(timestamp - t[probefunc]);
    t[probefunc] = 0;
}


How to run this script ?
Save this script in a file eg. myprobe.d and give executable permissions. But note that ONLY root user or user with equivalent role can execute the script and provide process id "PID" of the application as an argument.
Now run the script as below and see the results :
# myprobe.d <PID>

In future posts I will explain what more about the Dtrace in details. This was just an introduction to see what Dtrace can do.

Tuesday, December 10, 2013

Process and open files

There are situations where we may need to know :
1. Which files are opened by a given process
2. Which process is using a given file

If we are given a process and we need to find out the files opened by it then we can simply use
#pfiles <pid>

And if a file is given an we need to know which process has opened it then use
#fuser <file>

Convert shell script in binary

There are times when you want to hide the contents of your shell script to binary form so that it may not be read by others.
One such utility is called "shcomp"

shcomp - compile a ksh93 shell script

Using this utility you can compile/convert your ksh script to a binary script.