Office phones have to be online at office hours, otherwise a voicemail should be enabled. They should also be aware of holidays. A little perl-script checks for german holidays:

The dialplan

; extensions.conf
exten => 1234567,n,GotoIfTime(09:00-18:00,mon-fri,*,*?hcheck:vm)
exten => 1234567,n,GotoIfTime(09:00-14:00,sat,*,*?hcheck:vm)
exten => 1234567,n(hcheck),AGI(isholiday.pl)
exten => 1234567,n,GotoIf($[${ISHOLIDAY} = "YES"]?vm)
exten => 1234567,n(open),NoOp
...
exten => 1234567,n(vm),VoiceMail(1234567,sub)
...

The AGI:

#!/usr/bin/perl
# holiday check for asterisk
# /var/lib/asterisk/agi-bin/isholiday.pl

use strict;
my ($isholiday, $today,$ymd);

END { printf "SET VARIABLE ISHOLIDAY %s\n\n", ($isholiday)?"YES":"NO"; }

while(<STDIN>) {
     chomp;
     last unless length($_);
}

use Date::Holidays::DE qw(holidays);

my $feiertage_ref = holidays(
    WHERE    => [ 'common', 'ni'], # ni = Niedersachsen
    FORMAT   => "%F",
    YEAR     => 1900 + (localtime())[5],
    ADD      => [ 'heil', 'wei1', 'wei2',
                  'osts', 'ostm', 'pfis',
                  'pfim', '3okt', 'silv',
                  'mari' ]);

my (@ymd) = (localtime)[5,4,3];
$today = sprintf "%s-%s-%s", 1900+$ymd[0], 1+$ymd[1], $ymd[2];

for my $d (@$feiertage_ref) {
    if ($d eq $today) {
        $isholiday = 1;
        last;
    }
}
December 15th, 2012 | Tags: , , , , , ,

There is often a need to debug scripts which are started by daemons, so you have no input/output available. The following snippet easily redirects stdout and stderr to a file:

#!/bin/bash

exec >> /tmp/script_debug.log 2>&1
set -x 

echo Start: $(date)

: buggy code

echo Stop: $(date)

and here the output:

$ cat /tmp/script_debug.log
++ date
+ echo Start: Sat Dec 15 08:37:45 CET 2012
Start: Sat Dec 15 08:37:45 CET 2012
+ your-buggy-cmd
foo: line 8: your-buggy-cmd: command not found
++ date
+ echo Stop: Sat Dec 15 08:37:45 CET 2012
Stop: Sat Dec 15 08:37:45 CET 2012
November 12th, 2011 | Tags: , ,

The first part of Parallel Bash dealt with creation of parallel processes, so now it’s time for a real parallel tool.

The Apache benchmark tool ab has the disadvantage of sending only one URL for requests. To emulate a real server load we need some tools which can download several different files (.jpg, .js, .php, .css, ..) where each of them causes different CPU loads and network througphut, as well as different sizes of tranfered data. This can easily be done with httrack or wget.

And here is how:

The parallel routine pwork runs a site-mirror and collects the elapsed time in a log file

...
HTLOG=/tmp/httime.$$.log

pwork () {
    TMPDIR=$(mktemp -d /tmp/htbench_instance_XXXXX)
    TMPFILE=$(mktemp /tmp/htbench_log_XXXX)
    ( cd $TMPDIR
      #  append elapsed time to HTLOG
      /usr/bin/time -f "%e" -o $TMPFILE
      httrack -c8 --mirror --depth=2 -Q -q $1
      if lockfile -1 -r12 $HTLOCK; then
          cat $TMPFILE >> $HTLOG
      fi
      rm -f $HTLOCK $TMPFILE
    )
    rm -rf "$TMPDIR"
}
...

Limit the benchmark to n loops:

...
while [ $COUNT != 0 ]
do      J=$(jobs -p)
        echo "jobs left: "$COUNT
        N=$(echo $J|wc -w)
        if [ -z "$N" -o "$N" -lt $PARALLEL ]; then
            pwork "$URI" &
            (( COUNT-- ))
        fi
        sleep .2
done

Show final results:

...
show_results () {

    MINRUN=$(sort -n $HTLOG|head -1|cut -d' ' -f1)
    MAXRUN=$(sort -n $HTLOG|tail -1|cut -d' ' -f1)
    AVGRUN=$(perl -e '$sum=$count=0;
                         while (<>) {
                             chomp;$sum+=$_;$count++; }
                          printf "%.2fn", $sum/$count;' $HTLOG)
    rm $HTLOG

    echo "Benchmark Results"
    echo "================="
    echo
    echo "longest runtime:  "$MAXRUN
    echo "shortest runtime: "$MINRUN
    echo "average runtime:  "$AVGRUN
    echo
}

reaper () {
        echo "waiting to finish $RUNNING" >&2
        wait
        show_results
        exit 0
}

trap reaper 0 1 2 3 11 15
...


Use with care!

Download latest version:
htbench.sh

August 21st, 2011 | Tags: , , , ,

There is often the need to run several bash scripts in parallel. To achieve this, one need to keep track of the running processes. Here’s my solution:

#!/bin/bash

# parallel child processes
PP=5

if [ -n "$1" ]; then
        sleep $1
        exit
fi

reaper () {
        echo "waiting to finish" >&2
        wait
        exit 0
}
trap reaper 0 1 2 3 11 15

while :;
do      J=$(jobs -p)
        echo "running: "$J
        N=$(echo $J|wc -w)
        if [ -z "$N" -o "$N" -lt $PP ]; then
            $0 $[ $RANDOM % 8 ] &
        fi

        sleep .2
done

Paste this snippet to a file, and set the executable flag.

$ cat > parallel.sh
$ chmod +x parallel.sh

The output will look like this:

$ ./parallel.sh
running:
running: 25431
running: 25431 25438
running: 25431 25438 25445
running: 25431 25438 25445 25452
running: 25438 25445 25452 25459
running: 25445 25459 25466
running: 25445 25459 25466 25473
running: 25445 25459 25466 25480
running: 25445 25459 25466 25480 25487
running: 25459 25466 25480 25487
running: 25459 25466 25480 25487 25499
running: 25459 25466 25487 25499
running: 25459 25466 25487 25499 25511
running: 25466 25487 25499 25511
running: 25466 25487 25499 25511 25524
running: 25466 25487 25499 25511 25524
running: 25466 25487 25499 25524
running: 25487 25499 25524 25541
running: 25487 25499 25524 25541 25548
running: 25487 25524 25541 25548
running: 25524 25541 25560
running: 25524 25560 25567
running: 25524 25560 25567 25574
running: 25560 25567 25581
running: 25560 25567 25581 25588
running: 25560 25567 25581 25588 25595
^Cwaiting to finish
waiting to finish
$

Comments are welcome.

See also a similar solution of Grumbel: Parallel Bash

TOP