Ivor O’Connor

December 10, 2011

Demonizing

Filed under: Uncategorized — ioconnor @ 7:08 pm

The following is republished here so I can find it in the future and not have to worry about ever losing it.
#!/bin/bash

## ################################ ##
## ##
## daemon-functions.sh VERSION 0.1a ##
## ##
## ################################ ##

## Distrubuted under the GPL
## http://www.gnu.org/licenses/gpl-3.0.txt

## No warranty of any kind... May run
## off with your daughter. May explode
## in a ball of smoke and fire. Might
## work. Use at your own risk

## #!/bin/bash
##
## # Example Usage: datelogger.sh
## # A sample daemon which simply logs the
## # date and time once per second.
##
## function payload() {
## while [ true ]; do
## checkforterm
## date
## sleep 1
## done
## }
##
## source /path/to/daemon-functions.sh

function daemonize() {
echo $MY_PID > $MY_PIDFILE
exec 3>&- # close stdin
exec 2>>$MY_ERRFILE # redirect stderr
exec 1>>$MY_LOGFILE # redirect stdout
echo $(date)" Daemonizing" >> $MY_ERRFILE
}

function checkforterm() {
if [ -f $MY_KILLFILE ]; then
echo $(date)" Terminating gracefully" >> $MY_ERRFILE
rm $MY_PIDFILE
rm $MY_KILLFILE
kill $MY_PID
exit 0
fi
sleepcount=0
while [ -f $MY_WAITFILE ]; do
let sleepcount=$sleepcount+1
let pos=$sleepcount%10
if [ $pos -eq 0 ]; then
echo $(date)" Sleeping..."
echo $(date)" Sleeping..." >> $MY_ERRFILE
fi
if [ -f $MY_KILLFILE ]; then
rm $MY_WAITFILE
checkforterm
fi
sleep 1
done
}

MY_PID=$$
MY_PATH=$(readlink -f $0)
MY_ROOT=$(dirname $MY_PATH)
MY_NAME=$(basename $MY_PATH)
MY_PIDFILE="$MY_ROOT/.$MY_NAME.pid"
MY_KILLFILE="$MY_ROOT/.$MY_NAME.kill"
MY_ERRFILE="$MY_ROOT/.$MY_NAME.err"
MY_LOGFILE="$MY_ROOT/.$MY_NAME.log"
MY_WAITFILE="$MY_ROOT/.$MY_NAME.wait"
MY_BLOCKFILE="$MY_ROOT/.$MY_NAME.block"

CR="
"
SP=" "
OIFS=$IFS

case $1 in
pause)
touch $MY_WAITFILE
;;
resume)
rm $MY_WAITFILE
;;
restart)
$0 stop
$0 start
;;
start)
if [ -f $MY_BLOCKFILE ]; then
echo "Daemon execution has been disabled"
exit 0
fi
$0 run &
echo "Daemon Started"
exec 3>&- # close stdin
exec 2>&- # close stderr
exec 1>&- # close stdout
exit 0
;;
disable)
touch $MY_BLOCKFILE
$0 stop
;;
enable)
if [ -f $MY_BLOCKFILE ]; then rm $MY_BLOCKFILE; fi
;;
stop)
echo -n "Terminating daemon... "
$0 stat 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
echo "process is not running"
exit 0
fi
touch $MY_KILLFILE
$0 stat 1>/dev/null 2>/dev/null
ECODE=$?
waitcount=0
if [ "$waitcountmax" = "" ]; then waitcountmax=30; fi
while [ $ECODE -eq 0 ]; do
sleep 1
let waitcount=$waitcount+1
if [ $waitcount -lt $waitcountmax ]; then
$0 stat 1>/dev/null 2>/dev/null
ECODE=$?
else
ECODE=1
fi
done
$0 stat 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
PID=$(cat $MY_PIDFILE)
kill $PID
rm $MY_PIDFILE
rm $MY_KILLFILE
echo "Process Killed"
echo $(date)" Terminating forcefully" >> $MY_ERRFILE
exit 0;
else
echo "Process exited gracefully"
fi
;;
stat)
if [ -f $MY_BLOCKFILE ]; then
echo "Daemon execution disabled"
fi
if [ ! -f $MY_PIDFILE ]; then
echo "$MY_NAME is not running"
exit 1
fi
pgrep -l -f "$MY_NAME run" | grep -q -E "^$(cat $MY_PIDFILE) "
if [ $? -eq 0 ]; then
echo "$MY_NAME is running with PID "$($0 pid)
exit 0
else
echo "$MY_NAME is not running (PIDFILE mismatch)"
exit 1
fi
;;
log|stdout)
if [ -f $MY_LOGFILE ]; then
tail -f $MY_LOGFILE
else
echo "No stdout output yet"
fi
;;

err|stderr)
if [ -f $MY_ERRFILE ]; then
tail -f $MY_ERRFILE
else
echo "No stderr output yet"
fi
;;
pid)
if [ -f $MY_PIDFILE ]; then
cat $MY_PIDFILE
else
echo "No pidfile found"
fi
;;
run)
daemonize
payload
;;
help|?|--help|-h)
echo "Usage: $0 [ start | stop | restart | stat | pause | resume | disable | enable | (log|stdout) | (err|stderr) ]"
exit 0
;;
*)
echo "Invalid argument"
echo
$0 help
;;
esac

I got to this code from following the link here http://www.howtoforge.com/simple-bash-script-to-work-as-a-daemon to http://blog.apokalyptik.com/2008/05/09/as-close-to-a-real-daemon-as-bash-scripts-get/ which had the following code:

function payload() {
while [ true ]; do
checkforterm
date
sleep 1
done
}
source path/to/daemon-functions.sh

Google Voice vs Skype

Filed under: Uncategorized — ioconnor @ 6:20 pm

It’s not an either or between Google Voice and Skype. They are actually complimentary and both should be used in an office environment. Google Voice should be setup first and after GV is running Skype can be added just as if it were another cell phone.

Though Google voice has a big problem. It’s not solid and dependable. I’ve noticed many times situations where my cell phone will ring, Google Voice successfully routed the call, but did not also ring on my PC. So if I did not have a cell phone next to me I’d have missed the call. Also sometimes if you don’t have the GV and GMail windows open you will not get any alerts or rings. Having Skype fixes these problems because no matter what OS you have Skype will run on it. And Skype always pops up making it very easy to click on and avoid missing calls.

You can configure Skype to use the same number that you have ported to GV when making outgoing calls from Skype. Almost everything works seamlessly between the two. Both Skype and GV have excellent phone quality. The only wrinkle being Skype is not tied into GV’s address book. Skype attempts to use its own address book. This is fine for those Skype users who don’t have a real telephone number but for those that do Skype should use the GV directory. Because Skype always pops up in front of everything else you’ll be tempted to answer the call through Skype and won’t know who it is that’s calling. It’s a pain that Skype isn’t yet using GV’s address book.

So get them both and use Skype as a cheap backup, as another phone for GV to route to, for when Google Voice falls asleep at the job. Then configure Skype so that when it calls out it uses the main number that has been ported to GV.

GV is entirely free unless you ported your old phone number to it in which case it is a once only $20 fee. Skype costs about $60 a year to setup for unlimited dialing in the USA and Canada including a telephone number so that GV can route its incoming calls to Skype.

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.