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 Master Slave Replication in MySQL
Viewed 3546 times since Thu, Dec 26, 2013
How To Setup Ruby on Rails with Postgres
Viewed 11788 times since Mon, Dec 30, 2013
How to Setup and Configure an OpenVPN Server on Debian 6
Viewed 2619 times since Thu, Dec 26, 2013
How To Connect Node.js to a MongoDB Database
Viewed 7475 times since Mon, Dec 23, 2013
How To Install an Upstream Version of Node.js on Ubuntu 12.04
Viewed 2792 times since Sun, Dec 29, 2013
How To Set Up Apache Virtual Hosts on Ubuntu 12.04 LTS
Viewed 3705 times since Mon, Dec 23, 2013
How To Use Traceroute and MTR to Diagnose Network Issues
Viewed 6916 times since Fri, Dec 27, 2013
Initial Server Setup with Debian 7
Viewed 3840 times since Fri, Dec 27, 2013
How To Configure and Maintain Ghost from the Command Line
Viewed 6794 times since Sun, Dec 29, 2013
How To Create a SSL Certificate on Apache for Debian 7
Viewed 2588 times since Fri, Dec 27, 2013
|