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 Set Up mod_security with Apache on Debian/Ubuntu
Viewed 4063 times since Thu, Dec 26, 2013
Initial Server Setup with Debian 7
Viewed 3676 times since Fri, Dec 27, 2013
How To Use HAProxy to Set Up MySQL Load Balancing
Viewed 8194 times since Thu, Dec 26, 2013
Installing and Using the Vim Text Editor on a Cloud Server
Viewed 2520 times since Fri, Dec 27, 2013
How To Install Z Shell (zsh) on a Cloud Server
Viewed 2675 times since Fri, Dec 27, 2013
How To Install phpMyAdmin on a LEMP server
Viewed 2432 times since Thu, Dec 26, 2013
How To Install Linux, Nginx, MySQL, PHP (LEMP) Stack on Debian 7
Viewed 3864 times since Fri, Dec 27, 2013
How To Use Dokku Plugins to Access Additional Functionality
Viewed 4615 times since Sun, Dec 29, 2013
How To Setup a Rails 4 App With Apache and Passenger on CentOS 6
Viewed 3659 times since Thu, Dec 19, 2013
How To Create Data Queries in PostgreSQL By Using the Select Command
Viewed 14373 times since Mon, Dec 30, 2013
|