Register now and start sharing your code snippets.

A Ruby script that deletes images that are not used anywhere

Ruby posted 4 months ago by christian

   1  Dir.glob("**/*.{jpg,gif,jpeg,png}").sort.each do |image|
   2  
   3    name   = File.basename(image)
   4    in_use = false
   5    
   6    Dir.glob("**/*.{erb,css,js,html,htm}").each do |file|
   7      contents = File.read(file)
   8  
   9      in_use = contents.include?(name)
  10      
  11      break if in_use
  12    end
  13  
  14    if !in_use
  15      puts "Deleting '#{image}'"
  16      File.delete(image)
  17    end
  18  end

Tagged ruby, script, image

Recursively add files to ClearCase

Ruby posted 5 months ago by christian

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:

   1  %x{cleartool ls -view_only -r -s . > view_private_files.txt}
   2  
   3  lines = File.open('view_private_files.txt').readlines.collect{|line| %Q{"#{line.chomp}"} }
   4  
   5  # Work around command line length limit in Windows
   6  while lines.size > 0
   7    %x{cleardlg /addtosrc #{lines.slice!(0..100).join(' ')}}
   8  end

Next open a command line window and execute the script:

   1  cd clearcase_vob
   2  ruby add_recursively.rb

ClearCase sucks, use Mercurial or git instead…

Tagged add, recursive, clearcase, ruby, script

nginx startup script for Debian

Shell Script (Bash) posted 6 months ago by christian

   1  sudo vim /etc/init.d/nginx

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

   1  #! /bin/sh
   2  ##
   3  # nginx start script
   4  ##
   5  
   6  PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
   7  DAEMON=/usr/local/sbin/nginx
   8  NAME=nginx
   9  DESC=nginx
  10  
  11  if [ ! -x $DAEMON ]
  12  then
  13     echo "Couldn't find $DAEMON. Please set path to DAEMON."
  14     exit 0
  15  fi
  16  
  17  
  18  # Include nginx defaults if available
  19  if [ -f /etc/default/nginx ] ; then
  20  	. /etc/default/nginx
  21  fi
  22  
  23  set -e
  24  
  25  case "$1" in
  26    start)
  27  	echo -n "Starting $DESC: "
  28  	start-stop-daemon --start --pidfile /var/run/$NAME.pid \
  29  		--exec $DAEMON -- $DAEMON_OPTS
  30  	echo "$NAME."
  31  	;;
  32    stop)
  33  	echo -n "Stopping $DESC: "
  34  	start-stop-daemon --stop --pidfile /var/run/$NAME.pid \
  35  		--exec $DAEMON
  36  	echo "$NAME."
  37  	;;
  38    restart|force-reload)
  39  	echo -n "Restarting $DESC: "
  40  	start-stop-daemon --stop --pidfile \
  41  		/var/run/$NAME.pid --exec $DAEMON
  42  	sleep 1
  43  	start-stop-daemon --start --pidfile \
  44  		/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
  45  	echo "$NAME."
  46  	;;
  47    reload)
  48        echo -n "Reloading $DESC configuration: "
  49        start-stop-daemon --stop --signal HUP --pidfile /var/run/$NAME.pid \
  50            --exec $DAEMON
  51        echo "$NAME."
  52        ;;
  53    *)
  54  	N=/etc/init.d/$NAME
  55  	echo "Usage: $N {start|stop|restart|force-reload}" >&2
  56  	exit 1
  57  	;;
  58  esac
  59  
  60  exit 0

Now make the script executable with this command:

   1  sudo chmod 755 /etc/init.d/nginx

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

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

Tagged nginx, start, script, linux, debian