Wednesday, May 11, 2011

Find all arguments of a running process

Many processes have long command lines arguments, and it is sometime difficult to find all the arguments that the process was inintially started with.

unfortunately with the ps (/usr/bin/ps) command, users can only see the first 80 characters of command line, rest are ignored. So the user can not see the complete list of command line arguments.

Solution:
1. Use pargs argument (ex. ps -eaf | pargs -a )
or
2. Use /usr/ucb/ps command ( /usr/ucb/ps -awwx | grep )

Special Shell Variables

These are some variables which are set internally by the shell and are available to the user:

$1 - $9   These variables are the positional parameters.
$0        The name of the command currently being executed.
$#        The number of positional arguments given to this invocation of the shell.
$?        The exit status of the last command executed is given as a decimal string.
When a command completes successfully, it returns the exit status of 0
(zero), otherwise it returns a non-zero exit status.
$$        The process number of this shell - useful for including in filenames, to
make them unique.
$!        The process id of the last command run in the background.
$-        The current options supplied to this invocation of the shell.
$*        A string containing all the arguments to the shell, starting at $1.
$@@       Same as above, except when quoted.

Useful Vi Commands

The VI editor is a screen-based editor for Unix.
The VI editor has powerful features to aid programmers, but many beginning users avoid using VI because the different features overwhelm them.


General startup
To use vi: vi
To exit vi and save changes: ZZ or :wq
To exit vi without saving changes: :q!
To enter vi command mode: [esc]
Read the original file back in so that you can start over.: :e!


Counts
A number preceding any vi command tells vi to repeat that command that many times.
Deleting
x delete character under cursor
X delete character before cursor
dd delete line under cursor
dw delete word under cursor
db delete word before cursor
D delete to the end of line from the current cursor position
d^ delete from current cursor position to the beginning of the line
d$ delete from current cursor position to the end of the line

Copy/Paste Text
yy copies line
P paste copied data before cursor
p paste copied data after cursor

Find Commands
? finds a word going backwards
/ finds a word going forwards
f finds a character on the line under the cursor going forward
F finds a character on the line under the cursor going backwards
t find a character on the current line going forward and stop one character before it
T find a character on the current line going backward and stop one character before it
; repeat last f, F, t, T
, repeat last f, F, t, T going backwards
n repeat last search given by ‘/’ or ‘?’
N repeat last search given by ‘/’ or ‘?’ going backwards

Miscellaneous Commands
. repeat last command
u undoes last command issued
U undoes all commands on one line
xp deletes first character and inserts after second (swap)
J join current line with the next line
^G display current line number
% if at one parenthesis, will jump to its mate
mx mark current line with character x
'x find line marked with character x
^^ Go back to last file
READING/WRITING FILES
:r filename Copies filename after cursor in file currently editing
:n Start editing next file in list
:rew rewind file list, start editing 1st file on argument list
:w Saves the current file without quitting

MOVING
:# Move to line #
:$ Move to last line of file


Character/Line Formatting
~ Switch the case of the character under cursor
< Shifts the line upto where to the left by one shiftwidth
<< Shifts the current line to the left, and can be specified with a count
> Shifts the line upto where to the right by one shiftwidth
>> Shifts the current line to the right, and can be specified with a count
J Join the current line with the next one.

SHELL ESCAPE
:!'CMD' Executes CMD as a shell command
:!sh Fork shell in vi; hit ctrl-d to go back to vi

INSERTING
r replace character under cursor with next character typed
R keep replacing character until [esc] is hit
i insert before cursor
I insert from the beginning of line
a append after cursor
A append at end of line
o open line below cursor and enter append mode
O open line above cursor and enter append mode
c change until . "cc" changes the current line
C change to the end of line from the current cursor position
s substitute one character under the cursor and go into insert mode
S change an entire line


Screen Movement
G move to the last line in the file
xG move to line x
z+ move current line to top of screen
z move current line to the middle of screen
z- move current line to the bottom of screen
^F move forward one screen
^B move backward one line
^D move forward one half screen
^U move backward one half screen
^R redraw screen (does not work with vt100 type terminals)
^L redraw screen (does not work with Televideo terminals)

Cursor Movement
h move left
j move down
k move up
l move right
[return] move to the beginning of next line
$ last column on the current line
0 move cursor to the first column on the current line
^ move cursor to the first nonblank column on the current line
w move to the beginning of the previous word or punctuation
W move past the next space
b move to the beginning of the previous word or punctuation mark
B move to the beginning of the previous word, ignores punctuation
e end of next word or punctuation mark
E end of next word, ignoring punctuation
H move cursor to the top of the screen
M move cursor to the middle of the screen
L move cursor to the bottom of the screen
^H move cursor one space to the left
^J move cursor down one line in same column
^M move to the first character on the next line
^N move cursor down one line in same column
^P move cursor up one line in same column
% move cursor to the matching parenthesis or brace
( move cursor to the beginning of a sentence
) move cursor to the beginning of the next sentence
{ move cursor to the preceding paragraph
} move cursor to the next paragraph
| move cursor to the column specified by the count
+ move cursor to the first non-whitespace character in the next line
- move cursor to the first non-whitespace character in the previous line
_ move cursor to the first non-whitespace character in the current line

Shell script to capture cpu and memory usage of a process at certain intervals of time


Here is an automated shell script which can dump the memory and cpu usage of a process.
Useful when you want to monitor a process for a long period of time.

#!/bin/bash

if [ "$#" -ne 1 ]; then
echo "Usage ... [$0 ]"
exit
fi

pid=$1

# maxRunTime is used to control the maximum no of seconds for which to run this script.
maxRunTime=72000
# sleepTime is used to sleep for specified seconds, to take the memory dump again
sleepTime=30

logFile=${pid}_process.log
commandFile=${pid}_command.log

`ptree $pid >> $commandFile`;
`pargs $pid >> $commandFile`;

currentRunTime=0

while [ $currentRunTime -lt $maxRunTime ]
do
time=`date +%F::%X | tr -d '[\n]'`
line=`ps -eo pid,pcpu,vsz | grep $pid`;
echo "$time $line" >> $logFile
sleep $sleepTime
currentRunTime=`expr $currentRunTime + $sleepTime`
echo "Execution Time : ${currentRunTime} seconds [max ${maxRunTime}]"
done;

Monday, January 4, 2010

Solaris Tips

Find a process taking up a particular port :

for i in `ps -eawk '{print $1}'`; do echo $i; pfiles $i 2>/dev/null grep 'port: 1188'; done

This loop will go all the process and print the process id of the process which is consuming this particular port.

Useful oneliners:
Count total number of lines in all specific files under a directory : find . -type f -name '*.as' -o -name '*.mxml' -o -name '*.java' xargs cat wc -l
Find number of occurrences of a text in a file : grep text fileName wc -l
Display the top most process utilizing most CPU (top -b 1)
Show the working directory of a process : (pwdx pid )
Display the parent/child tree of a process : (ptree pid )
Display the no.of active established connections to localhost : netstat -a grep ESTABLISHED
Display the largest files/directories in order of size: du -sk * sort -nr
Display the files in the directory by file size : (ls -ltr sort -nr -k 5)
Display the all files recursively with path under current directory : ( find . -depth -print)
To save man pages to a file use : man col -b > filename

Friday, June 26, 2009

Perl Errors

Missing Module in array @INC

(use perl -e 'print join("\n", @INC);' to print the list of directories in @INC)

Errors like: Can't locate TimeDateNum.pm in @INC (@INC contains: /usr/local/lib/perl5/5.8.5/i86pc-solaris /usr/local/lib/perl5/5.8.5 /usr/local/lib/perl5/site_perl/5.8.5/i86pc-solaris /usr/local/lib/perl5/site_perl/5.8.5 /usr/local/lib/perl5/site_perl .) at ./test.pl line 5.

Solution:

We need to provide the missing module to perl. It can be done in the following ways.
1. Specify it in the PERL5LIB environment variable
export PERL5LIB="path of required module"

2. Use the -I option to specify the additional directories where it should search for modules while invoking perl.
perl -I "path of required module" script.pl

3. Modify your Perl program to find the module by adding the below line at the top of your program.

use lib "path of required module";

Perl adds this directory to it's @INC search list.


Thursday, June 25, 2009

sed Samples

Quick Examples:

General Syntax:
sed -option 'general expression' [data-file]
sed -option sed-script-file [data-file]

Option can be:

OptionMeaningExample
-eRead the different sed command from command line.
$ sed -e 'sed-commands' data-file-name
$ sed -e 's/test/TEST/' File
$ sed -e

-fRead the sed command from sed script file.
$sed -f
$ sed -f chgdb.sed friends.tdb
-nSuppress the output of sed command. When -n is used you must use p command of print flag to explicitly specify what is to be printed.$ sed -n '/^\*..$/p'

Search and replace:

$ sed 's/Linux/UNIX/' TestFile > file.out

here 's' is the search command and '/' is the delimiter used to separate the text to be searched and replaced.

Deleting blank lines

Using sed you can delete all blank line from file as follow
$ sed '/^$/d' demofile1
Here the pattern /^$/, match blank line and d, command deletes the blank line.

Using Regular Expressions with sed

$ sed -n '/10\{2\}1/p' TestFile

prints 1001, search pattern \{2\}.
Syntax:
\{n,\}
At least nth occurrences will be matched. So /10\{2\} will look for 1 followed by 0 (zero) and \{2\}, tells sed look for 0 (zero) for twice.

Matching any number of occurrence

Syntax:
\{n,\m}
Matches any number of occurrence between n and m.

Example:
$ sed -n '/10\{2,4\}1/p' TestFile2
1001
10001
100001
Will match "1001", "10001", "100001" but not "101" or "10000000".

$ sed -n '/^\*..$/p' demofile2
***
***
Above command prints all lines that begins with *** (three stars or asterisks),

Explanation:
Command
Explanation
^Beginning of line
\* Find the asterisk or star (\ remove the special meaning of '*' metacharacter)
..

Followed by any two character (you can also use \*\* i.e. $ sed -n '/^\*\*\*$/p' demofile2 )

$

End of line (So that only three star or asterisk will be matched)

/pPrint the pattern.

Also the following expression can be used for the same purpose
$ sed -n '/^\*\{2,3\}$/p' TestFile2

Following command finds out lines between *** and *** and then delete all those line
$sed -e '/^\*\{2,3\}$/,/^\*\{2,3\}$/d' demofile2 > /tmp/fi.$$
$cat /tmp/fi.$$


Above expression can be explained as follows

ExpressionMeaning
^Beginning of line
\* Find the asterisk or star (\ remove the special meaning of '*' metacharacter)
\{2,3\}Find next two asterisk
$End of line
,Next range or search pattern
^\*\{2,3\}$Same as above
dNow delete all lines between *** and *** range