Skip to content

Instantly share code, notes, and snippets.

@vls
Created July 16, 2011 02:32
Show Gist options
  • Save vls/1085926 to your computer and use it in GitHub Desktop.
Save vls/1085926 to your computer and use it in GitHub Desktop.
some common utility functions
function log()
{
local msg=$1
now=`date "+%Y-%m-%d %H:%M:%S"`
i=${#FUNCNAME[@]}
lineno=${BASH_LINENO[$i-2]}
file=${BASH_SOURCE[$i-1]}
echo "${now} ${file}:${lineno} ${msg}"
}
function check_running()
{
if [ $# -ne 1 ]; then
log "must specify a pid file"
exit 1
fi
local pidfile=$1
if [ ! -f $pidfile ]; then
touch $pidfile
fi
ps -p `cat ${pidfile}` > /dev/null
if [ $? -eq 0 ]; then
log "already running"
exit 0
else
log "I'm running $$"
echo $$ > ${pidfile}
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment