Wednesday, November 25, 2015

Display cricket score on Unix Terminal

If your job demands you to be on Unix console all the time and you happen to be one who can not avoid watching the cricket score, something you would like:
The script:
#Get cricket score on your unix console top corner, enjoy cricket while you work ..

columns=$(tput cols)

while :
  line=$(wget -q -O- $matchurl  | awk -F '=' '/og:title/ {print $3}' | awk -F '|' '{print $1}' | sed 's/"//g')
  echo -en "\033[s"    #save current screen position & attributes
  tput cup 0 $startpoint
  echo -en "\033[42m$line\033[0m"
  echo -e -n "\033[u"
  sleep 15

Friday, November 6, 2015

Bash - Get function name inside a function

$FUNCNAME has the function name in a bash script which can be used for useful purpose like the following:

WORKID=$(date +%s) ; mkdir $WORKID
LOGFILE=$WORKID/$(basename $0).log

_f_log() {
        echo "$(date):$@" >> $LOGFILE

_f_testFunc() {
        local logfile=$1
        local status=$2
        _f_log "$FUNCNAME:$logfile:$status"


_f_testFunc /tmp/a.log restart
Executing it:
$ ./
$ cat 1446805714/
Fri Nov  6 15:58:34 IST 2015:_f_testFunc:/tmp/a.log:restart

Saturday, March 7, 2015

Unix - run multiple commands on remote machines in parallel

Assume you have a requirement of running two commands on 10 hosts.

In this post I would like to show you how you can:

1) Run multiple commands on remote host using ssh.

2) Also instead of working on one host at a time serially, how can we execute the commands on multiple hosts in parallel to save the overall execution time.

Find the number of files under '/sdb/_read/' and '/sdb/_write/' directories on following 10 hosts.

$ cat host.txt

Serial execution: 
We can write a bash for loop and work on each host like this:

$ for ip in $(cat host.txt); do echo -n "$ip|"; ssh -2 root@$ip 'R=$(ls /sdb/_read/ | wc -l ) ; D=$(ls /sdb/_write/ | wc -l) ; echo "$R|$D"'; done

Time taken:
real 0m50.084s
user 0m0.052s
sys 0m0.032s

Parallel execution:
I have already blogged about Unix xargs parallel execution option here, using that:

$ cat host.txt  | xargs -n 1 -P 5 -i ssh -2 root@{} 'I=$(hostname -i);R=$(ls /sdb/_read/ | wc -l ) ; D=$(ls /sdb/_write/ | wc -l)  ; echo "$I|$R|$D"'

Time taken:
real 0m10.437s
user 0m0.044s
sys 0m0.036s

10 Seconds vs 50 Seconds, wow !


Note: Utilities like GNU parallel or Parallel SSH are always the best tool in this job.

Related posts:

  1. Unix Xargs paralel execution
  2. Unix - Execute script on remote host using SSH without copying the script.
  3. Diff remote files using SSH in Unix
  4. Bash and SSH some examples

© Jadu Saikia