Home » Categories » Multiple Categories |
How To Write a Linux Daemon with Node.js |
Article Number: 225 | Rating: 1/5 from 1 votes | Last Updated: Sun, Jan 5, 2014 at 8:49 PM
|
IntroductionA daemon is a program that runs in background and has no controlling terminal. They are often used to provide background services. For example, a web-server or a database server can run as a daemon.This tutorial will show you how to write a daemon with Node.js and deploy it on your server with Upstart. I will focus on implementing a standard daemon. I'm using upstart just for simplicity, however you can write a System-V init script or use anything you like to start your daemon. RequirementsFor this tutorial, you will need a Linux server(Preferably Ubuntu or CentOS), Node.js, and Upstart. Node.jsThere are several ways to install Node.js. The easiest way in my opinion is to use nvm. It also allows you to manage different versions of Node. Alternatively, you can follow one of these guides:
UpstartUpstart comes pre-installed on many Linux distros. If it's not installed on the distro of your choice, you should be able to install it from official repositories. You can also compile it from the source-code. Refer to Upstart Getting Started page for more info. How Daemons WorkBasically a daemon starts like a normal process. the following will occur afterwards:
Instead of closing the standard file descriptors, the parent process can open the null device and attach it to the child's standard file descriptors. The Example DaemonFor the sake of this tutorial, we're going to create a simple HTTP daemon.Our daemon will be able to:
Initial project structureThe following is the initial project structure:
package.jsonLet's start by creating a package.json file:
Install daemon module:
And here is how the package.json should look like:
Note that we added a start script, So we can start the app with HTTP ServerFor now, we're going to create a simple HTTP server that starts listening on port 80 and responds with "Hello world" for every request. Put the following in lib/app.js file:
You can start the server by running Daemon executableThe following is the daemon executable code. The following should be placed in bin/node-simple-http-daemon:
Time to start our daemon! But before that, we should modify our app.js to Add the following to the end of the app.js file:
Make the daemon script executable:
And run it (Make sure nothing else is running on port 80):
Now, your daemon and it's workers should be running in background. You can
Before moving to the next step, let's take a look at the process IDs:
Sample output:
Note that the daemon is the process with parent-pid of 1. Try restarting the workers by sending SIGHUP to the daemon:
Now if you list the PIDs again, you should be able to see new worker processes with new PIDs. Amazing, isn't it? To stop the daemon, just run:
Dropping privilegesWe're almost done. We only need to make workers drop their privileges after the server gets started. Modify
And that's it for the daemon part. Now you can install it system-wide:
UpstartMaking an Upstart job is very easy. Create a file in/etc/init/node-simple-http-daemon.conf with the following contents:
Now you can:
Next StepsYou may want to modify the daemon to give up spawning workers when they're getting killed too frequently (spinning).I can't emphasize enough how important it is to have adequate logging in your application. Make sure to check out node-bunyan. |
Attachments
There are no attachments for this article.
|
How To Install Z Shell (zsh) on a Cloud Server
Viewed 2395 times since Fri, Dec 27, 2013
How To Migrate a MySQL Database Between Two Servers
Viewed 2664 times since Thu, Dec 26, 2013
How To Install Node.js with NVM (Node Version Manager) on Server
Viewed 3718 times since Sun, Dec 29, 2013
How To Install And Run A Node.js App On Centos 6.4 64bit
Viewed 10188 times since Sun, Dec 29, 2013
How To Configure and Maintain Ghost from the Command Line
Viewed 6383 times since Sun, Dec 29, 2013
Geddy.JS: A No-Brainer MVC Node.js Framework
Viewed 3024 times since Sun, Dec 29, 2013
How To Install Wordpress on Ubuntu 12.04
Viewed 2387 times since Tue, Dec 24, 2013
How To Create, Remove, & Manage Tables in PostgreSQL on a Cloud Server
Viewed 3115 times since Mon, Dec 30, 2013
Intermediate Sed: Manipulating Streams of Text in a Linux Environment
Viewed 9659 times since Fri, Dec 27, 2013
How To Set Up an NFS Mount on Ubuntu 12.04
Viewed 10087 times since Tue, Dec 24, 2013
|