Archive for the ‘DIY’ Category

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

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