How to exlude grep from a ps pipe result

Posted: October 3, 2012 in DIY, Linux, script
Tags: , , ,

How to exlude grep from a ps pipe result

Hello guys,

This is a short post of something I find very usefull if you have to do “ps -ef” often.

When you do a ” server#ps -ef | grep ‘something’ ” on any server you can se a line that also includes the grep process.

root@yourdiy.net:/# ps -ef | grep something
something      1084     1  0 Oct02 tty4     00:00:00 /sbin/getty 38400 tty4
something      1085     1  0 Oct02 tty5     00:00:00 /sbin/getty 38400 tty5
something      1086     1  0 Oct02 tty6     00:00:00 /sbin/getty 38400 tty6
something     20064 19246  0 09:44 pts/0    00:00:00 su
something     20109 20064  0 09:46 pts/0    00:01:21 bash
something     20748   883  0 17:28 ?        00:00:00 sleep 60
root     20750 20109  0 17:28 pts/0    00:00:00 grep something
root@yourdiy.net:/# 

You can see the  last line that includes the commands we just used, to avoid this you can simply use a negative form of grep.

 

root@yourdiy.net:/# ps -ef | grep something | grep -v "grep something"
something      1084     1  0 Oct02 tty4     00:00:00 /sbin/getty 38400 tty4
something      1085     1  0 Oct02 tty5     00:00:00 /sbin/getty 38400 tty5
something      1086     1  0 Oct02 tty6     00:00:00 /sbin/getty 38400 tty6
something     20064 19246  0 09:44 pts/0    00:00:00 su
something     20109 20064  0 09:46 pts/0    00:01:21 bash
something     20748   883  0 17:28 ?        00:00:00 sleep 60
root@yourdiy.net:/#

 

Like this you can see that the grep process is not longer listed, this is very usefull if you are using the output in a script.
I hope this little info help you.

Regards

File Growth Check Script

Posted: July 7, 2011 in CLI, Linux

Hello Guys,

This little script is for cecking on a file growth, and letting us know when the file has finished growing, more updates soon of course but here is the first release. ENJOY


#!/bin/bash
# File Growth Check Script
# Created by wsanriv 7/7/2011
# First input is file Path
# Example: "fgc.sh /tmp/file1"

#Input File path
if [ $# -lt 1 ]
then
    echo "Please give file"
    exit 1
fi
FILE_IN=$1

#Check for empty path or non file path
if [ ! -f $FILE_IN ]
then
    echo "Not a valid file"
    exit 1
fi

#Sample initialization
sample_a=0
tic=0

#Loop for Growth check
while true
    do
    clear
    sample_b=$sample_a
    ls -alh $FILE_IN | grep $FILE_IN
    sleep 1
#---Check if growth stoped
    sample_a=$(stat -c%s ${FILE_IN})
    if [ $sample_a -eq $sample_b ]
    then
    echo "SAME SIZE"
    $tic = ${tic} + 1
    fi
    done

#END

Hello Guys,

I just finished a very funny script to get the current bandwidth utilization of a Linux/Unix ethernet card.
This is just the first draft I have to make tons of changes, and options. but this is the main body the first working copy.

As always any comments are appreciated.

#!/bin/bash

#Function to get bit sample
get_sample()
{
/sbin/ifconfig eth0 | grep 'RX bytes:' | awk ' { split($2, RX, ":"); print RX[2]}'
}

#Function to calculate the difference between 2 samples
calc_sample()
{
A=$(get_sample)
sleep 1
B=$(get_sample)
echo $(($B - $A))
}

#Function to make the output more human readable
humanize()
{
if [ $((($RESULT * 8)/1000)) -gt 1000 ]
then
echo 0
else
echo 1
fi
}

#Main script
while true; do
RESULT=$(calc_sample)
#This checks if the output is readable or not
if [ $(humanize) -eq 0 ]
then
echo $(((($RESULT * 8)/1000)/10000))
echo "Megabits per second"
else
echo $((($RESULT * 8)/1000))
echo "Kilobits per second"
fi
#Sleep 5 secs before asking for new sample
sleep 5
done

Network Speed

Network Speed

Linux/Unix Ping State Script

Posted: April 21, 2011 in CLI, DIY, Linux

Hello Guys,

After a good afternoon of work, I bring you a small script to check on server network status.
You know “Keep Alive”  style.
This small script is intended to review on how many responses do you get from a ping test.
and you can use the out put for what ever you like, like an php report.

This is the script code:

root@bobafett:/# cat /pingok.sh
#!/bin/bash
HOST=$1
COUNT=4
if [ $# -eq 0 ]; then
        HOST=localhost
fi
count=$(ping -c $COUNT $HOST 2> /dev/null | grep 'received' | awk -F',' '{print $2 }' | awk '{ print $1 }')
if [ -z $count ]; then
        echo "Invalid Output"
        exit 1
fi
#echo $HOST #THIS IS DEBUGING
if [ $count -eq 4 ]; then
        # 100% Worked
        echo "UP"
fi
if [ $count -eq 3 ]; then
        # 25% failed
        echo "H/LOAD"
fi
if [ $count -eq 2 ]; then
        # 50% failed
        echo "INTERMIT"
fi
if [ $count -eq 1 ]; then
        # 75% failed
        echo "DEPRECATED"
fi
if [ $count -eq 0 ]; then
        # 100% failed
        echo "DOWN"
fi

This is a screenshot of how it looks in a PHP report.

I cant copy the entire PHP code, but I will be more than glad to share if you want it.

PHP Report Example

View Hosts

Posted: March 3, 2011 in CLI, Windows

Hello guys,

Today I come with a very fast but convenient shortcut this one to be used to view on the hosts file quickly, as I am usually tweaking with it.

@echo off
type c:\windows\system32\drivers\etc\hosts

This little 2 lines allow you to view the hosts file in console very fast.

just remember to save the .bat file with this 2 lines in the  “c:\windows\system32” so no matter from where you type the command it will be always executed.

I hope this helps anyone that wants to use it.

Cya later!

Ping Connection Test with Sound

Posted: February 8, 2011 in CLI, Linux, Windows

Hello Guys,

I just finished a draft for a Ping Connection Test that emits a sound when its Disconnected, I like this to monitor my internet even if I am not using it, also more actions can be done not just the sound, this is just a start, this is the first draft of the code, I will be posting a step by step details later.

@echo off
:A
set destination=%1
ping %destination% -n 1 |FIND /C “TTL” > c:/temp.log
set /p result=< c:/temp.log
IF %result% EQU 0 (
echo “Connection Inactive”
type beep.txt
ping 0.0.0.0 -w 1000 > NUL
GOTO A
)
IF %result% EQU 1 (
echo “Connection Active”
ping 0.0.0.0 -w 100000 > NUL
)
GOTO A

As I said this is just a basic draft later on I will post how to create you own and how to run it.

Hello all,

This miniature batch is just a random number generator that works between 0 and 32767.
What is does.
Takes 2 inputs %1 (Minimum Number) and %2 (Maximum Number)
Then it runs %random%  operation until the returns a number between the range you specified, there are 2 if´s that work as limiter to ensure the batch does not loop.
Please do remember that if you create a batch and want to run it as a command you have to put it in “c:/windows/system32/” and named as command.bat this will allow you to run the command in CMD, this Random Number Generator runs like this.
Note:  Low ranges may take more time as the chance to get a number in that range is lower.

RNG?Example

Code:

@echo off
set minimum=%1
set maximum=%2
if %maximum% LEQ %minimum% (
echo Maximum is lower than Minimum
goto B
)
if %maximum% GEQ 32767 (
echo Maximum number is to big please use 32767 or below)
:A
set randnumber=%random%
if %randnumber% GEQ %minimum% (
if %randnumber% LEQ %maximum% (
echo %randnumber%
goto END
)
)
goto A
:END

My plan is to make an more interactive version, but not for now, I just needed a quick random command.
Remember that necessity always gives us a chance to create.

Regards!
Will San