Buzz: A Node.js Command Line Program to Keep Your App Running Indefinitely; Like the Program Forever

Buzz is a command line program that can kill your app routinely and restart it.
It’ll will also restart your app if it dies. It’s a lot like the other Node.js
program Forever.

It’s much simpler than Forever. Approximately 50 lines of CoffeeScript code.
It displays your apps output to STDOUT and also displays any of your apps
STDERR output in red.

Usage

Install it via npm:

npm install buzz

Then run:

buzz 240 your_cool_app param1 param2

The first parameter to buzz is the time in seconds that it’ll be killed and
restarted. So, `your_cool_app` would be killed and restarted after four minutes.

If you don’t want buzz to kill your app, but you want it to bring it back to
life if it dies, run:

buzz your_cool_app param1 param2

You can test buzz by running his the app `buzz_test`:

buzz_test

`buzz_test` runs the app `smarty_pants` that spews out random facts to you and
taunts you. Occasionally `smarty_pants` will commit suicide, but buzz will
bring him back to life.

`buzz_test` ends up actualy just running the following command:

buzz 10 smarty_pants 2000 0.15

Which will kill smarty pants every 10 seconds and bring him back to life. Also,
every two seconds, smarty pants will spit out a random fact. Approximately, every
13 seconds smarty pants will take his own life, but Buzz will bring him back.

Motivation

I have a command line app that is nasty to debug. It’s working fine for the first
five minutes or so. Thus, Buzz was born. Instead of fixing the bug, I wanted
to make this. =)

But really, it’s utility is that it’s a much simpler Forever.

The name comes from Buzz Lightyear in the movie Toy Story. His popular phrase was: To infinity and beyond!

Do you use Git? If so, checkout Gitpilot to make using Git thoughtless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

Installing MongoDB 1.8.1 on Ubuntu 10.04 LTS

A lot of this information will seem obvious to a lot of you. But it did require me to perform a few Google searches, so hopefully I can save you the trouble.

I provide a script to automate this process:

This script is deprecated now. Fortunately 10Gen (the company who makes MongoDB) provides an Apt repository. Do not use the current MongoDB version in the Ubuntu apt sources. It’s version 1.6.*

All you need to do:

vim /etc/apt/sources.list

Add the following line:
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen


apt-get update
apt-get install mongodb-10gen

By default MongoDB runs in trusted mode. This means that it’s listening on port 27017 for any connection. We want it to only listen on localhost.

Edit the file: /etc/mongodb.conf

Add the following lines:
bind_ip 127.0.0.1
noauth = true ##### YOU MUST add this line if you use bind_ip

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

That’s it. Follow me on Twitter: @jprichardson and read my blog on software entrepreneurship.

-JP

Iterate Over Files in Bash

Sometimes you need to whip up a dirty bash script to process through some text. I needed to iterate over my database log files that I wrote about from my last post and then output the result to a new file. I wanted the output files to have a different extension, but I wasn’t sure how to get just the filename from bash, fortunately this post helped.

#!/bin/bash

for logfile in *.log
do

fn=${logfile%.*} #cut extension

echo making $fn.data...
egrep '\#\ collect|\:\=' $logfile > $fn.data 

done

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

Follow me on Twitter @jprichardson and read my blog on software entrepreneurship.

-JP

Grep for Two or More Expressions

Unfortunately a database of mine crashed. Fortunately, I have log files with the data. I need the timestamp and the data.

The lines in the log files look something like this:

####### collect $TIMESTAMP #########
*GARBAGE
*GARBAGE
*GARBAGE
$ID:=$DATASET
*GARBAGE

It’s been years since I’ve done anything more than a simple grep. I wasn’t even sure which grep I needed… egrep? fgrep? grep?

I can use this regex for the timestamp line:

\#\ collect

I can use this regex for the dataset line:

\:\=

But how can I put them together? It’s probably of no surpise that I can use the ‘|’ operator like so:

egrep '\#\ collect|\:\=' 2010-09-25.log

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

That’s it! Follow me on Twitter @jprichardson and read my blog on entrepreneurship.

-JP

Backup a MySQL Database on Ubuntu

Backing up a MySQL database is extremely simple. Just simply create a directory as to where you want to store your backups. I typically just dump them in /var/dbbak. You can then run the following command:

/usr/bin/mysqldump -u root -p YOUR_DB_NAME | gzip > /root/dbbak/YOUR_DB_NAME_`date +%y_%m_%d`.gz

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

-JP

Deploying a Rails application on Ubuntu 8.04 LTS with Phusion Passenger and SQL Server 2005

This article will provide step by step instructions for preparing and then deploying a Rails application on Ubuntu 8.04 LTS that uses Microsoft SQL Server 2005.

Install Ruby
sudo apt-get install ruby-full build-essential

Install Apache 2
sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev

Install Ruby Gems
Do not install Ruby Gems from Apt.  This will screw up your Gems repository as Apt and Gems will both want to manage it.  Download the latest Gems package from http://rubyforge.org.  At the time of this writing the latest version is 1.3.  Version 1.3 is also the version that I used.
wget http://rubyforge.org/frs/download.php/43985/rubygems-1.3.0.tgz
tar xzvf rubygems-1.3.0.tgz
cd rubygems-1.3.0
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
sudo gem update --system

Install Rails
Make sure to install the proper version.  You may need to pass the “–version” flag.
sudo gem install rails

Install Phusion Passenger
This allows us to deploy Rails application with as much simplicity as it takes to deploy a PHP application.  This we can avoid using Mongrel/Proxy Balancing/(Apache|Lighttpd|Nginx).
sudo gem install passenger
sudo passenger-install-apache2-module

Next add the following to your /etc/apache2/apache2.conf file:
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3
PassengerRuby /usr/bin/ruby1.8

Install Ruby/ODBC/SQLServer2005 Compatibility
In /etc/profile add the following:
export ODBCINI=/etc/odbc.ini
export ODBCSYSINI=/etc
export FREETDSCONF=/etc/freetds/freetds.conf

Make sure you restart your shell after this!

Now install UnixODBC:
sudo apt-get install unixodbc
sudo apt-get install unixodbc-dev
sudo apt-get install tdsodbc

Now install FreeTDS (stable) from source. The FreeTDS in the Apt repository is version 0.63 and will not work with SQL Server 2005. At the time of this writing, the current stable version is 0.82.
wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
cd freetds-stable
./configure
make
sudo make install

Edit /etc/freetds/freetds.conf and add the following:
[YOUR_DB_DEFINITION_NAME]
host = 172.24.40.100
port = 1433
tds version = 8.0

Test it with the following:
sqsh -S YOUR_DB_DEFINITION_NAME -U USERNAME -P PASSWORD

Once actually logged in, you can run some SQL and verify the results. Make sure to type “go” after each command and also make sure to “use” the right database.

Now edit /etc/odbc.ini and add the following:
[YOUR_DB_DEFINITION_NAME]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = YOUR_DB_DEFINITION_NAME
Database = YOUR_ACTUAL_DB_NAME

Edit /etc/odbcinst.ini and add the following:
[FreeTDS]
Description = TDS driver (Sybase/Microsoft SQL)
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 1

Now test it….
isql YOUR_DB_DEFINITION_NAME USERNAME PASSWORD

Install Ruby ODBC:
wget http://www.ch-werner.de/rubyodbc/ruby-odbc-0.9995.tar.gz
tar zvxf ruby-odbc-0.9995.tar.gz
cd ruby-odbc-0.9995/
ruby extconf.rb
make
sudo make install

Install ActiveRecord ODBC Adapter
sudo gem install activerecord-odbc-adapter

Edit your database.yml in your application and configure it like…
development:
adapter: odbc
dsn: YOUR_DB_DEFINITION_NAME
username: USERNAME
password: PASSWORD

Now dump your Rails app in /var/www/yourrailsapp and add a new Apache Virtual Host as seen in the previous post. Configure the virtual host DocumentRoot to point to the public directory in Apache2.  Do not forget to change ownership of your rails app to www-data. (chown -R www-data:www-data yourrailsapp) That’s all there is too it! Now you can deploy a number of Rails apps in Apache with little trouble!

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

Apache Virtual Hosting

This is extremely easy to do.  This was done on Ubuntu 8.04.  Replace “SERVER_IP” with the IP address of your  server.

Edit /etc/apache2/apache2.conf and add the following:

NameVirtualHost SERVER_IP:80

Create a new file “subdomain” in /etc/apache2/sites-available with the content:

<VirtualHost SERVER_IP:80> *

ServerName subdomain.yourdomain.com
DocumentRoot /var/www/subdomain

<Directory “/var/www/subdomain”> **
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

Then run:
a2ensite subdomain
/etc/init.d/apache2 restart

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

Follow

Get every new post delivered to your Inbox.