#!/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 }