All posts by a.philippi

unicorn start/stop and monitoring script

Today I wanted to write a script which could be used to start/stop/restart unicorn for a given application – without root privileges, to get a status and a special action(monitor) which can be called periodically and will restart unicorn automatically in case something went wrong

I found this very usefull blogpost on the net http://rubynyc.wordpress.com/2009/12/24/unicorn-scriptspin/ and modified the script with 2 additional “methods”: status and monitor. (I know … the code I added is pretty primitive… but it works)


#!/usr/local/bin/ruby

UNICORN_RAILS='/usr/local/bin/unicorn_rails'
APP_PATH="#{ARGV[0]}"

class Runner
class << self def start system "cd #{APP_PATH}; #{UNICORN_RAILS} -c ./config/unicorn.rb -E production -D" end def reload system "kill -s USR2 #{pid}" end def restart stop start end def graceful_stop system "kill -s QUIT #{pid}" end def stop system "kill #{pid}" end def pid File.read "#{APP_PATH}/tmp/pids/unicorn.pid" end def status begin system "ps -o user,pid,ppid,command -ax | grep #{pid}" rescue puts 'not started' end end #start if killed def monitor begin #this throws an exception in 2 cases: #1. if no unicorn-process for the current user exists #2. if the pid functions fails(throws an error) due to non-existant unicorn.pid-file throw 'processes killed manually' if (`ps -o user,pid,ppid,command -ax | grep #{pid}`).split("\n").size <= 2 rescue puts 'restarting ... ' start end end end end case ARGV[1] when "start" Runner.start when "reload" Runner.reload when "restart" Runner.restart when "stop" Runner.stop when "status" Runner.status when "monitor" Runner.monitor else STDERR.puts "usage ./unicorn_script.rb absolute_path_to_RAILS_ROOT [start|stop|restart|status|monitor]" exit(1) end

This script can be called from a cron every minute like this


* * * * * /path/to/this_script /path/to/rails_root/ monitor

It baiscally checks if the unicorn-processes exist(the master process and his spawned kids). If it does not detect at least one unicorn-process it will calls the start routine.

crontab weirdness … schedule a job to run on first saturday of every month

We wanted to schedule a backup script to run on the first saturday of every month at 13:10 on a debian lenny host.

The syntax we used in crontab was:
10 13 1-7 * 6 /path/to/myscript.sh

But because of the cron-implementation the script was running on every saturday AND on the 01.-07. of every month.
This is not a bug, but a “feature” – I found the following documentation on wikpedia http://en.wikipedia.org/wiki/Cron:

if both “day of month” and “day of week” are restricted (not “*”), then either the “day of month” field (3) or the “day of week” field (5) must match the current day (even though the other of the two fields need not match the current day).

The resolution for me was to enforce the time check in the command line:
10 13 * * 6 if [ $(date +\%d) -lt 7 ]; then /path/to/myscript.sh; fi

Apache: password protect directories except from local ips

We faced the following problem:

1. we want to let our clients test projects on our test webserver – using basic http authentication.

2. users from our local network should not be bothered by the username and password request

The solution we implemented:

Place a .htaccess – file in the webroot of your apache webserver with the following content:

Order deny,allow
Deny from all
AuthName "htaccess password prompt"
AuthUserFile /path/to/your/webroot/.htpasswd
AuthType Basic
Require valid-user
Allow from localhost #that would be me
Allow from 192.168.0.0/24 #suppose the ip range of your local network goes from 192.168.0.2 - 192.168.0.254
Satisfy Any

Use htpasswd to create the file AuthUserFile /path/to/your/webroot/.htpasswd and create at least one user with a password:

htpasswd -c /path/to/your/webroot/.htpasswd username
#you will be promtped for a password

Remarks:

1.depending on your local network configuration you want might to exclude the internal router/gateway IP from the range of IPs(192.168.0.0/24) that do not require authentication

2.of course, the apache virtualhost must be configured in such a manner that .htacces-files are evaluated. Take a look at the AllowOverride directive

avidemux ROCKS

There are a lot of video editing/ripping/encoding, etc… tools for Linux OS.

Some of them are missing basic functions, others have extremely complicated user interfaces with tons of parameters that have to be specified even for the most simple transformation.

Avidemux has an intuitive UI, can convert/encode/export your video in nearly every possible format.

I use avidemux to convert videos recorded with my digital camera (which has little processing power and therefore does a very weak compression) to MPEG-4 ASP(Xvid) – format using a two-pass encoding which allows setting a targeted video size.

Give it a try:
sudo apt-get install avidemux

cumulative CPU time for a given process

Sometimes you want to know how much CPU time has been used by a given process – let’s say the named-daemon
Of course you can use top , but usually you would have to add columns, sort/filter by user or command name, etc…

You can simply use:
ps -A -o pid,comm,cputime | grep named

This will give you all processes that contain named in their command name(in my case one single named-process was running), showing their pid, command name, and cumulative cpu time, like:

20129 named 05:27:11

2010 – Year of DEATH … for IE6 – i hope

Best wishes for 2010 … more time for you, your families + friends 🙂 … and a slow and painfull death for IE6!!!

IE6 (Microsoft Internet Explorer – version 6) tortures thousands of web-developers for years.  Every web-interface needed a lot of  “special hacks” to be rendered reasonably on IE6.

Think of the IE box model bug, the  CSS underscore hack, the <!–[if lte IE 6]>-like Syntaxes for including js or css only for IE6, IFrames to cover select boxes when using a absolutely-positioned div, etc…

The statistics of Factureaza.RO show the following trend of IE6 usage:

  • june 2009: 21%
  • july 2009: 22%
  • august 2009: 19%
  • september 2009: 17%
  • october 2009: 18%
  • november 2009: 18%
  • december 2009: 17%
  • january 2010: 16%

Let us put an end to this agony and join the guys at end6.org. Just include the piece of JS offered by them – and your visitors will be advised to upgrade ther browser.

<!–[if gte IE 7]>
alert(‘Happy new year’);
<![endif]–>

presenting the team – senior newbie #2

Cosmin Piţu – Junior Ruby on Rails Developer

At my workstation
At my workstation

Joined the team about two months ago, trying to find the “way it’s meant to be done” approach to Rails projects – have discovered Rails last year about this time, developed some small projects yet always learned “by ear” so not necessarily best practices / using Rails’ whole potential. Always on the prowl for inspiration to match my allegedly semi-artsy persona.

This is me by my workplace, never mind the “visual noise” there, I usually keep my desk oh so very tidy ! And yes, as Andreas said on the first few days of work, I’ve been given the largest monitor in the office (Jakob thought the resolution and the crispness of the said monitor were below-par so I accepted the little gift ). And oh yes, that bash shell I’m rocking is totally Matrix-style.

Yes, I’m usually a bit more “outspoken” than this, but I guess this will suffice for the time being, a small “intro” post. Check out my profile for more I guess.
“Keep up keeping up” !