Archive for februari, 2013

Genomen stappen

  1. SD kaart image gemaakt van raspberry wheezy
  2. Raspberry config (fill sd card, time locale, memory use)
  3. Stappen voor node.js + npm (incl git essentials)
  4. map / file   aanmaken / var/www/app.js
  5. forever geinstalleerd (forever start app.js)
  6. stappen voor crontab (opstarten bij reboot) (vergeet niet te chmod 777)
  7. socket.io lukt niet om te installeren sofar

Read Full Post »


Some time ago, I wrote a post on keeping Node.js apps running even after logging out from the shell. It was cool and all, but soon you will soon realize that Forever alone is not enough. Because, on system reboots, Forever itself is killed. Who will restart your app now?

There are many options for handling this kind of scenario, but today we will implement a solution using cron. Let’s find out how we can make Forever and our Node.js app reboot-proof.

The app you want Forever to start on system restart is probably a web app, so let’s try this thing on an Express.js web app.

Install Express, if you haven’t already:

$ npm install -g express

Now, create an Express app:

$ mkdir example
$ cd example
$ express

There we have our Express app. It should be started using Forever – on system reboots – automatically.

First we need to write a bash script to check if our app is running or not; if it is not running, it should start the app using Forever. Create a file named app-starter.sh in your home directory with this content:


if [ $(ps aux | grep $USER | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
        export NODE_ENV=production
        export PATH=/usr/local/bin:$PATH
        forever start ~/example/app.js > /dev/null

Note how we augmented the environment variables in the script. Those are crucial to making the script work successfully from cron. Change the NODE_ENV variable, according to your requirement./usr/local/bin needs to be added to PATH, else Forever just won’t work on most systems.

Next, make the script executable:

$ chmod 700 ~/app-starter.sh

Then, run it and load http://localhost:3000 on your browser to confirm it actually started our app using Forever:

$ ./app-starter.sh

Looking good. Now it’s time to make cron do the job for us. We want to make sure that the app-starter script is execute when the system is rebooted. Append the following code to crontab:

@reboot ~/app-starter.sh >> cron.log 2>&1
# guess what the commented line below would do if enabled
# */1 * * * * ~/app-starter.sh >> cron.log 2>&1

To open crontab:

$ crontab -e

After making changes to crontab, restart your system.

Load http://localhost:3000 on your browser to behold the magic of Forever + cron :D Now you don’t have to worry about system restarts taking your app down, anymore. cron takes care of Forever, and Forever takes care of your app; and you win at life!

While this example is perfect for a single app, you will need to hack the app-starter script to make it work for more than one Node.js app. How to do that is your homework. All the best!

Read Full Post »

If the Ubuntu Server installer has set your server to use DHCP, you will want to change it to a static IP address so that people can actually use it.

Changing this setting without a GUI will require some text editing, but that’s classic linux, right?

Let’s open up the /etc/network/interfaces file. I’m going to use vi, but you can choose a different editor

sudo vi /etc/network/interfaces

For the primary interface, which is usually eth0, you will see these lines:

auto eth0
iface eth0 inet dhcp

As you can see, it’s using DHCP right now. We are going to change dhcp to static, and then there are a number of options that should be added below it. Obviously you’d customize this to your network.

auto eth0
iface eth0 inet static

Now we’ll need to add in the DNS settings by editing the resolv.conf file:

sudo vi /etc/resolv.conf

On the line ‘name server xxx.xxx.xxx.xxx’ replace the x with the IP of your name server. (You can do ifconfig /all to find out what they are)

You need to also remove the dhcp client for this to stick (thanks to Peter for noticing). You might need to remove dhcp-client3 instead.


sudo apt-get remove dhcp-client


Now we’ll just need to restart the networking components:

sudo /etc/init.d/networking restart

Ping http://www.google.com. If you get a response, name resolution is working(unless of course if google is in your hosts file).

Really pretty simple.

Read Full Post »

These instructions work for the Raspberry Pi running Raspbian (hard float) and create a hardware optimized version of NodeJS for the Raspberry PI, (and include a working install and NPM!!!):

1) Install Raspbian – http://www.raspberrypi.org/downloads

2) Install the necessary dependecies:

sudo apt-get install git-core build-essential

(If you just installed git then you need to administer your git identity first, else adding the patches below will fail!!!)

3) Check out Node.js source (0.8.8 or higher):

git clone https://github.com/joyent/node.git
cd node
git checkout v0.8.8 -b v0.8.8

4) Push four patches for V8 and OpenSSL ARM support:

The following two patches are only needed when compiling Node version <0.8.10 (already committed for 0.8.10pre NOT yet for master)

Remark: The first patch is already commited upstream to V8 cutting-edge branch (node is on a lower version of V8). The 2nd patch is pushed to the V8 bug list.

curl https://github.com/joyent/node/commit/25c2940a08453ec206268f5e86cc520b06194d88.patch | git am
curl https://github.com/joyent/node/commit/1d52968d1dbd04053356d62dc0804bcc68deed8a.patch | git am

The following two patches are only needed when compiling Node version <0.8.9 (already committed for master and 0.8.9pre + higher)

curl https://github.com/joyent/node/commit/f8fd9aca8bd01fa7226e1abe75a5bcf903f287ab.patch | git am
curl https://github.com/joyent/node/commit/7142b260c6c299fc9697e6554620be47a6f622a9.patch | git am

5) Configure correctly and ‘make’:


6) (Optional!) If you want to do the tests then execute (make test can’t be used because the standard timeout is too low, i.e. the Rasp PI is too slow… ;-)):

python tools/test.py -t 120 --mode=release simple message

7) Install Node and NPM in your OS:

sudo make install

NOTE on 0.8.8 tests: some tests still have issues (8 tests fail, some are timeouts (as the PI isn’t that fast 😉 and one is a known bug)!

Read Full Post »

Note: Some users have reported issues with using Mac OS X to create SD cards.

  1. These commands and actions need to be performed from an account that has administrator privileges.
  2. Download the image from a mirror or torrent
  3. Verify if the the hash key is the same (optional), in the terminal run:
    • shasum ~/Downloads/2012-12-16-wheezy-raspbian.zip
  4. Extract the image:
    • unzip ~/Downloads/2012-12-16-wheezy-raspbian.zip
    • (or: just double click the zip, it will extract automatically)
  5. From the terminal run df -h
  6. Connect the SD card reader with the SD card inside
  7. Run df -h again and look for the new device that wasn’t listed last time. Record the device name of the filesystem’s partition, for example, /dev/disk3s1
  8. Unmount the partition so that you will be allowed to overwrite the disk:
    • sudo diskutil unmount /dev/disk3s1
    • (or: open Disk Utility and unmount the partition of the SD card (do not eject it, or you have to reconnect it)
  9. Using the device name of the partition work out the raw device name for the entire disk, by omitting the final “s1” and replacing “disk” with “rdisk” (this is very important: you will lose all data on the hard drive on your computer if you get the wrong device name). Make sure the device name is the name of the whole SD card as described above, not just a partition of it (for example, rdisk3, not rdisk3s1. Similarly you might have another SD drive name/number like rdisk2 or rdisk4, etc. — recheck by using the df -h command both before & after you insert your SD card reader into your Mac if you have any doubts!):
    • For example, /dev/disk3s1 => /dev/rdisk3
  10. In the terminal write the image to the card with this command, using the raw disk device name from above (read carefully the above step, to be sure you use the correct rdisk# here!):
    • sudo dd bs=1m if=~/Downloads/2012-10-28-wheezy-raspbian/2012-12-16-wheezy-raspbian.img of=/dev/rdisk3
    • if the above command report an error(dd: bs: illegal numeric value), please change bs=1M to bs=1m
    • (note that dd will not feedback any information until there is an error or it is finished, information will show and disk will re-mount when complete. However if you are curious as to the progresss – ctrl-T (SIGINFO, the status argument of your tty) will display some en-route statistics).
  11. After the dd command finishes, eject the card:
    • sudo diskutil eject /dev/rdisk3
    • (or: open Disk Utility and eject the SD card)
  12. Insert it in the Raspberry Pi, and have fun

Read Full Post »