Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Tim Ferrell http://www.s0nspark.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Memcached StartupItem for Mac OS X


Here is an OS X StartupItem for memcached...

/Library/StartupItems/Memcached/StartupParameters.plist
{
  Provides        = ("Memcached");
  Description     = "Memcache Daemon";
  Uses            = ("Network");
  OrderPreference = "None";
}


/Library/StartupItems/Memcached/Memcached
#!/bin/bash
#
# /Library/StartupItems/Memcached/Memcached
#
# Script to startup memcached with OS X. Tested on 10.4 Tiger
#
# To enable, copy this file and StartupParameters.plist to
# /Library/StartupItems/Memcached and add "MEMCACHED=-YES-"
# to /etc/hostconfig ... You can then reboot or execute 
# "sudo /sbin/SystemStarter start Memcached" from a terminal
# to start it up.
#
# I should mention that this file uses my preferred development 
# settings. You can edit them before copying this script over 
# or they can be overriden by creating a config file named 
# memcached.conf in /etc, /opt/etc, or /usr/local/etc with the 
# following contents (modified to suit, of course):
#
#   MEMCACHED_EXE=/usr/local/bin/memcached
#   MEMCACHED_PIDFILE=/var/run/memcached.pid
#   MEMCACHED_MAX_MEM=128
#   MEMCACHED_INTERFACE=127.0.0.1
#   MEMCACHED_PORT=1121
#   MEMCACHED_RUN_AS=nobody 
#
# ------------------------------------------------------------------- 
# NOTE: Memcached < 1.2.0 has issues specifying the interface via the
# '-l' switch on the Mac for some reason so if the service fails to 
# start with the following error:
#
#    bind(): Can't assign requested address
#    failed to listen
#
# then you will need to either upgrade to version 1.2.0+ or forgo
# specifying an interface to listen on by removing the references
# to A_INTERFACE on lines 65 and 75 of this script. 
# ------------------------------------------------------------------- 
#
# Tim Ferrell <s0nspark@gmail.com>
#

# Suppress the annoying "$1: unbound variable" error 
if [ -z $1 ] ; then
  echo "Usage: $0 [start|stop|restart] "
  exit 1
fi

# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
source /etc/rc.common

NAME=memcached
DESC="Memcached server"

# look for a config file name ${NAME}.conf in /etc, /opt/etc, 
# and /usr/local/etc ... in that order.
[ -r /etc/${NAME}.conf ] && source /etc/${NAME}.conf
[ -r /opt/etc/${NAME}.conf ] && source /opt/etc/${NAME}.conf
[ -r /usr/local/etc/${NAME}.conf ] && source /usr/local/etc/${NAME}.conf

# these are _my_ defaults... 
DAEMON="${MEMCACHED_EXE:=/usr/local/bin/memcached}"
A_PIDFILE="-P ${MEMCACHED_PIDFILE:=/var/run/memcached.pid}"
A_MAXMEM="-m ${MEMCACHED_MAX_MEM:=128}"
A_INTERFACE="-l ${MEMCACHED_INTERFACE:=127.0.0.1}"
A_PORT="-p ${MEMCACHED_PORT:=11211}"
A_RUNAS="-u ${MEMCACHED_RUN_AS:=nobody}"

StartService () 
{
  if [ "${MEMCACHED:=-NO-}" = "-YES-" ] && ! GetPID ${NAME} > /dev/null; then
    if [ -f /var/run/${NAME}.StartupItem ] ; then exit ; fi
    touch /var/run/${NAME}.StartupItem
    echo "Starting ${DESC}"
    ${DAEMON} -d ${A_PIDFILE} ${A_MAXMEM} ${A_INTERFACE} ${A_PORT} ${A_RUNAS} 
  fi
}

StopService ()
{
  if PID=$(GetPID ${NAME}); then
    echo "Stopping ${DESC}, (PID ${PID})"
    kill -TERM "${PID}"
    rm -f ${A_PIDFILE}
  else
    echo "${DESC} not running."
  fi
  rm -f /var/run/${NAME}.StartupItem
}

RestartService () { StopService; StartService; }

if test -x $DAEMON ; then
  RunService "$1"
else
  echo "Could not find ${DAEMON}!"
fi



Capitalizing titles in Ruby - another take...

This code was inspired by Peter's code from May 17...

I basically extended his idea to be more flexible and added ! methods to modify the string in place. I chose to use a different regex selector because using "\b" breaks on apostrophes and leave you with words like "You'Re" instead of "You're"...


class String

  def titlecase()
    ignore_list = %w{of etc and by the for on is at to but nor or a via}
    capitalize_all_ex(ignore_list)
  end

  def titlecase!()
    ignore_list = %w{of etc and by the for on is at to but nor or a via}
    capitalize_all_ex!(ignore_list)
  end

  def capitalize_all(force_downcase = true)
    ignore_list = %w{}
    capitalize_all_ex(ignore_list, force_downcase)
  end

  def capitalize_all!(force_downcase = true)
    ignore_list = %w{}
    capitalize_all_ex!(ignore_list, force_downcase)
  end

  def capitalize_all_ex(ignore_list, force_downcase = true)
    # if force_downcase is true then the 
    # string is, um, downcased first :-)
    if force_downcase
      self.downcase.gsub(/[\w\']+/){ |w| 
        ignore_list.include?(w) ? w : w.capitalize  
      }
    else
      self.gsub(/[\w\']+/){ |w| 
        ignore_list.include?(w) ? w : w.capitalize  
      }
    end
  end

  def capitalize_all_ex!(ignore_list, force_downcase = true)
    if force_downcase
      self.replace(self.downcase.gsub(/[\w\']+/){ |w| 
        ignore_list.include?(w) ? w : w.capitalize  
      })
    else
      self.replace(self.gsub(/[\w\']+/){ |w| 
        ignore_list.include?(w) ? w : w.capitalize  
      })
    end
  end
end

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS