quinta-feira, 28 de março de 2013

Long Repo Sync

Ever had to do a very loooooong repo sync?

What if it breaks in the middle by network problems?

Created a little script to help you with that.

It will download 1 project at a time minimizing errors.
It logs every project synced in a "log.txt" with a status beside each one.

If you have errors , just re-execute the script , it will read the "log.txt" and try to resync bad projects.

All you need to do is run the script in the folder where .repo was created.


#!/bin/bash

FILE=".repo/manifest.xml"

if [ -e "./log.txt" ] ; then
        cat ./log.txt | grep Error >> newlog.txt
        while read line ; do
                check=`echo $line | grep "Error"`
                if [ ! -z "$check" ] ; then
                        name=`echo $line | awk '{ print $2 }'`
                        echo "Resyncing project=$name"
                        repo sync $name > output.txt 2>&1
                        check=`cat output.txt | grep "From ssh:"`
                        if [ ! -z "$check" ] ; then
                                end=`cat output.txt  | grep "fatal"`
                                if [ -z "$end" ] ; then
                                        echo "Project= $name Status=Done" >> log.txt
                                else
                                        echo "Project= $name Status=Error" >> log.txt
                                fi
                        else
                                echo "Project= $name Status=Already Synced" >> log.txt
                        fi

                fi
        done < ./newlog.txt
        rm -f ./log.txt ./newlog.txt
fi

if [ -e "$FILE" ] ; then
    NPROJ=`cat $FILE | grep project | wc | awk '{ print $1}'`
    i=1
    while read line ; do
        PROJECT=`echo $line | grep "<project"`

        if [ ! -z  "$PROJECT" ] ; then
                name=$(grep -Po '(?<=name=")[^"]*' <<<$line )
                echo "Syncing project=$name  ($i/$NPROJ  $((100*i/$NPROJ))%)" 
                repo sync $name > output.txt 2>&1              
                check=`cat output.txt | grep "From ssh:"`
                if [ ! -z "$check" ] ; then
                        end=`cat output.txt  | grep "fatal"`
                        if [ -z "$end" ] ; then
                                echo "Project= $name Status=Done" >> log.txt
                        else
                                echo "Project= $name Status=Error" >> log.txt
                        fi
                else
                        echo "Project= $name Status=Already Synced" >> log.txt
                fi
                i=$((i+1))
        fi
   done < $FILE
fi

quarta-feira, 27 de março de 2013

Killall!!!!!

Have you ever been frustrated when there are millions of unwanted processes running in a server and you need to kill them all?
And to make matters worse "killall" just doesn't seem to work!!!

I made this little script to solve this issue , you can kill any process with a given string in its name or any processes from a given user


#!/bin/bash

if [ "$1" == "-u" ] ; then
        PID=`cat /etc/passwd | grep "$2" | cut -d ":" -f3`
        processes=`ps aux | grep "$PID" | egrep -v "PID|ps \-au|killbyname|grep" | awk '{ print $2}'`
        echo "############# Killing all processes of user: $2 ############################"
else
        echo "############# Killing processes by name: $1 ############################"
        processes=`ps aux | grep "$1" | egrep -v "killbyname|grep" | awk '{ print $2}' `
fi


for process in $processes ; do
        command=`ps aux | grep $process | egrep -v "grep" | awk '{ print $2 }'`
        echo "Killing process: $process"
        echo ""
        kill -9 $process
done

USAGE:

./killbyname.sh "name"

Will kill any process with "name" in its running path , important to remember that if the process has "name" in any part of its running path it will be killed.

./killbyname.sh -u "username"

Will kill any processes being run by the specified username.

Feel free to improve it!