script snippets

nginx startup script for Debian

Tagged nginx, start, script, linux, debian  Languages bash
sudo vim /etc/init.d/nginx

Paste in the following (remember to run 'set :paste' in VIM when pasting):

#! /bin/sh
##
# nginx start script
##

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

if [ ! -x $DAEMON ]
then
   echo "Couldn't find $DAEMON. Please set path to DAEMON."
   exit 0
fi


# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
    . /etc/default/nginx
fi

set -e

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --pidfile /var/run/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --pidfile /var/run/$NAME.pid \
        --exec $DAEMON
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --pidfile \
        /var/run/$NAME.pid --exec $DAEMON
    sleep 1
    start-stop-daemon --start --pidfile \
        /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
    echo "$NAME."
    ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --pidfile /var/run/$NAME.pid \
          --exec $DAEMON
      echo "$NAME."
      ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

Now make the script executable with this command:

sudo chmod 755 /etc/init.d/nginx

Lastly, run this command to make the script run when the server starts and stops:

sudo /usr/sbin/update-rc.d -f nginx defaults

Recursively add files to ClearCase

Tagged add, recursive, clearcase, ruby, script  Languages ruby

This script adds all files in the current directory to ClearCase.

Save the following script as add_recursively.rb in the directory you want to add to ClearCase:

%x{cleartool ls -view_only -r -s . > view_private_files.txt}

lines = File.open('view_private_files.txt').readlines.collect{|line| %Q{"#{line.chomp}"} }

# Work around command line length limit in Windows
while lines.size > 0
  %x{cleardlg /addtosrc #{lines.slice!(0..100).join(' ')}}
end

Next open a command line window and execute the script:

cd clearcase_vob
ruby add_recursively.rb

ClearCase sucks, use Mercurial or git instead...

A Ruby script that deletes images that are not used anywhere

Tagged ruby, script, image  Languages ruby
Dir.glob("**/*.{jpg,gif,jpeg,png}").sort.each do |image|

  name   = File.basename(image)
  in_use = false
  
  Dir.glob("**/*.{erb,css,js,html,htm}").each do |file|
    contents = File.read(file)

    in_use = contents.include?(name)
    
    break if in_use
  end

  if !in_use
    puts "Deleting '#{image}'"
    File.delete(image)
  end
end

Simple script for adding existing project to git/gitosis

Tagged git, gitosis, script  Languages bash
GIT_USER=git
SERVER=xxx.com
APP=xxx

touch tmp/.gitignore log/.gitignore vendor/.gitignore
find . -type d -empty | grep -v "vendor" | grep -v ".git" | grep -v "tmp" | xargs -I xxx touch xxx/.gitignore

cat <<EOF >>.gitignore
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
*.swp
Capfile
EOF

git init
git add .
git commit -m "initial commit" 
git remote add origin $GIT_USER@$SERVER:$APP.git
git push origin master

Fontello script

Tagged fontello, makefile, script  Languages makefile

Easily download and install Fontello font icons with this makefile; no more manual downloading and copying:

#
# Easily download and install Fontello font icons.
#
# Original version:
# https://gist.github.com/puzrin/5537065
#
 
# Note this directory will be deleted
TMP_DIR      ?= ./tmp/fontello
# The Fontello config.json file
CONF_FILE    ?= ./config/fontello.json
# Where do you want the font files to be?
FONT_DIR     ?= ./public/font
# Where do you want the CSS files to be?
CSS_DIR      ?= ./app/css/vendor
 
### Don't edit below ###
 
FONTELLO_HOST ?= http://fontello.com
 
download:
    @if test ! which curl ; then \
        echo 'Install curl first.' >&2 ; \
        exit 128 ; \
        fi
    curl --silent --show-error --fail --output .fontello \
        --form "config=@${CONF_FILE}" \
        ${FONTELLO_HOST}
    open ${FONTELLO_HOST}/cat .fontello

install:
    @if test ! which unzip ; then \
        echo 'Install unzip first.' >&2 ; \
        exit 128 ; \
        fi
    @if test ! -e .fontello ; then \
        echo 'Run make fontopen first.' >&2 ; \
        exit 128 ; \
        fi
    rm -rf ${TMP_DIR} .fontello.zip
    curl --silent --show-error --fail --output .fontello.zip \
        ${FONTELLO_HOST}/cat .fontello/get
    unzip .fontello.zip -d ${TMP_DIR}
    mkdir -p ${FONT_DIR}
    mkdir -p ${CSS_DIR}
    cp -R ${TMP_DIR}/*/font/* ${FONT_DIR}
    cp -R ${TMP_DIR}/*/css/* ${CSS_DIR}
    rm -rf ${TMP_DIR} .fontello.zip