Do not forget to buy it – if you use it for commercial purpose.
You can get it for Windows, Max, and… tadaaaa… Linux.
Category Archives: linux
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.
howto restart networking on FreeBSD
Linux users are used to something like:
sudo /etc/init.d/networking restart
which does the entire job … usually.
On FreeBSD you have to restart the routing tables/service separately like this:
sudo /etc/rc.d/netif restart; sudo /etc/rc.d/routing restart
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