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 Setup a Rails 4 App With Apache and Passenger on CentOS 6
Viewed 3731 times since Thu, Dec 19, 2013
How To Configure and Maintain Ghost from the Command Line
Viewed 6678 times since Sun, Dec 29, 2013
How To Use a Simple Bash Script To Restart Server Programs
Viewed 3295 times since Fri, Dec 27, 2013
How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Debian
Viewed 7828 times since Thu, Dec 26, 2013
Upgrading Debian to Unstable
Viewed 3677 times since Thu, Dec 26, 2013
How To Install (LEMP) nginx, MySQL, PHP stack on Arch Linux
Viewed 13709 times since Sun, Dec 29, 2013
How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Arch Linux
Viewed 4601 times since Fri, Dec 27, 2013
How To Manage Packages In Ubuntu and Debian With Apt-Get & Apt-Cache
Viewed 6847 times since Fri, Dec 27, 2013
How To Import and Export Databases and Reset a Root Password in MySQL
Viewed 2952 times since Thu, Dec 26, 2013
How To Protect SSH with fail2ban on Debian 7
Viewed 2746 times since Fri, Dec 27, 2013
|