Monday, March 08, 2010

BAD and BETTER COMMANDS

BAD and BETTER COMMANDS

I cringe anytime I see someone code inefficiently.  Here are
three of the most common mistakes, followed by a better way to
do the same thing.

Bad:    cat somefile | grep something
Better: grep something somefile
Why:    You're running one program (grep) instead of two (cat
and grep).

Bad:    ps -ef | grep something | grep -v grep
Better: ps -ef | grep [s]omething
Why:    You’re running two commands (grep) instead of three (ps
and two greps).

Bad:    cat /dev/null > somefile
Better: > somefile
Why:    You're running a command (cat) with I/O redirection,
instead of just redirection.

Although the bad way will have the same result, the good way is
far faster.  This may seem trivial, but the benefits will really
show when dealing with large files or loops.

Saturday, February 06, 2010

I SED BLANK

I SED BLANK

Using sed, you can remove blank lines, and
lines that contain only whitespace, from a
file using the following:

sed -e '/^[     ]*$/d' InputFile >OutputFile

Within the single quotes ('), the forward
slashes (/) delimit the regular expression
that will be interpreted by sed.  The "d"
before the closing single quote, tells sed
to delete any lines that match the regular
expression.

Within the regular expression, the caret
(^) matches the beginning of a line.
The []* matches zero to many occurrences of
the character list between the open bracket
([) and the close bracket (]) (in the above
regular expression, you must insert a space
and a tab between the brackets).  The dollar
sign ($) matches the end of a line.

These three constructs together match any
blank line or any line that contains only
spaces and tabs (in any combination).

Since the standard operation of sed is to
echo lines to stdout, all lines except blank
lines (or lines that only contain whitespace)
will be sent to OutputFile.





Saturday, January 30, 2010

KILLING ALL THE PROCESS FOR A PARTICULAR USER

KILLING ALL THE PROCESS FOR A PARTICULAR USER

To kill all processes of a particular user from root
at unix prompt type:

# kill -9 `ps -fu username |awk '{ print $2 }'|grep -v PID`

We can also use the username as an argument and pass it from
command line, if this command is put as a script.

This command will not disconnect nor logout the user though in 
the system. 

REBOOTING BECAUSE OF FORK BOMBS

REBOOTING BECAUSE OF FORK BOMBS


There is nothing more frustrating
for an Administrator who has to
reboot system due to fork bomb

(the number of processes in the
system reaches the maximum limit
when a user, even a superuser,
tries to execute some command, the
system will respond with Vfork
failed)

In Solaris under SPARC, this can be
controlled by specifying a line in
/etc/system

set maxuprc=64

And reboot the system. Now a user
can have maximum of 64 processes
under his ownership. By default
the 'maxuprc' value is
16*maxusers - 5 where
'maxusers' is another tunable
parameter in /etc/system

Caution : You should have a backup
of /etc/system file before you make
the changes. So that you can revert
back to old system file using
boot -a option in case of
inconsistent system file.

LOCK DOWN TELNET OR FTP

LOCK DOWN TELNET OR FTP

When inbound access isn't required into
a system deny users Telnet or FTP access
do the following:

vi /etc/inetd.conf

Comment the line starts with Telnet or
FTP. Save the file and exit.

Stop and start the inetd daemon now by
following commands:

/etc/rc.d/init.d/inet stop

/etc/rc.d/init.d/inet start

(Your flavor may be /etc/init.d)

Now on nobody can telnet or FTP to your
server from outside network.