From 67b0ec5741d327d27afe88783d85fcf2c4c4f204 Mon Sep 17 00:00:00 2001 From: David Runge Date: Sun, 3 Nov 2013 12:30:45 +0100 Subject: First commit --- bin/functions.sh | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100755 bin/functions.sh (limited to 'bin/functions.sh') diff --git a/bin/functions.sh b/bin/functions.sh new file mode 100755 index 0000000..951def5 --- /dev/null +++ b/bin/functions.sh @@ -0,0 +1,176 @@ +#!/bin/bash +# some functions + +# zsh color output +# echo "$FG[46]$FX[blink]Hello, World" + +function echo_green() { + echo -e '\E[1;32m'$*'\E[0m' +} + +function echo_red() { + echo -e '\E[1;31m'$*'\E[0m' +} + +function echo_magenta() { + echo -e '\E[1;35m'$*'\E[0m' +} + +function echo_cyan() { + echo -e '\E[1;36m'$*'\E[0m' +} + +# log text ... +function log() { + date=`date "+%Y-%m-%d %H:%M:%S"` + if [ -n $VERBOSE ] + then + echo $date $@ | tee -a $LOGFILE + fi + [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE +} + +function log_green() { + date=`date "+%Y-%m-%d %H:%M:%S"` + if [ -n $VERBOSE ] + then + echo_green $date $@ + fi + [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE +} + +function log_red() { + date=`date "+%Y-%m-%d %H:%M:%S"` + if [ -n $VERBOSE ] + then + echo_red $date $@ + fi + [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE +} + +function log_magenta() { + date=`date "+%Y-%m-%d %H:%M:%S"` + if [ -n $VERBOSE ] + then + echo_magenta $date $@ + fi + [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE +} + +function log_cyan() { + date=`date "+%Y-%m-%d %H:%M:%S"` + if [ -n $VERBOSE ] + then + echo_cyan $date $@ + fi + [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE +} + +function notify() { + title="$1" + text="$2" + echo 'naughty.notify({title = "'$title'", text = "'$text'"})' | awesome-client +} + +# mute command +function mute() { + $@ > /dev/null 2> /dev/null +} + +# run pidfile command +function run() { + pidfile=$1 + shift + command=$@ + + exec $command > /dev/null 2> /dev/null & + pid=$! + sleep 0.25 + npid=$(pidof -s $1) + if [ -n "$npid" ] && [ $npid -ne $pid ] + then + pid=$npid + fi + echo $pid > $pidfile + log_green executed $command"\n" +} + +# run_once $pidfile $command[] +function run_once() { + pidfile=/tmp/run/lock/$1 + shift + command=$@ + + mkdir -p $(dirname $pidfile) + + if [ -f $pidfile ]; + then + pid=`head -n 1 $pidfile` + running=$(ps -u $(whoami) -p $pid | grep '^[ ]*'$pid) + if [ -z "$running" ] + then + log_cyan stale pidfile found + rm -f $pidfile + + npid=$(pidof -s $1) + if [ -n "$npid" ] + then + log_green "pidfile updated\n" + echo $npid > $pidfile + else + run $pidfile $command + fi + + else + log_cyan "active pidfile found\n" + fi + else + pid=`ps | grep "$command" | grep -v grep | head -n 1 | awk '{ print $1 }'` + if [ -z "$pid" ] + then + log_green not running + run $pidfile $command + else + log_cyan "pidfile updated\n" + echo $pid > $pidfile + fi + fi +} + +# run_auto $file +function run_auto() { + file=$1 + . $file + + cur_host=`hostname` + case $cur_host in + annoyance|silence|remembrance) + cur_area=private + ;; + c*|s*) + cur_area=unixpool + ;; + esac + + if [ $area != $cur_area -a $area != "any" ] + then + log_magenta invalid_area $(basename $file)"\n" + return + fi + + if [ $host != $cur_host -a $host != "any" ] + then + log_magenta invalid_host $(basename $file)"\n" + return + fi + + if [ $allow_multiple -eq 1 ] + then + log_green run $(basename $file) + run /dev/null $command + else + log_green run_once $(basename $file) + run_once `basename $file` $command + fi +} + -- cgit v1.2.3-54-g00ecf