Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

64 lines (46 sloc) 1.348 kB
* Daemon.node
*** A node.JS addon that allows creating Unix/Linux Daemons in pure Javascript.
*** Copyright 2010 (c) <>
* Under MIT License. See LICENSE file.
#include <v8.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ev.h>
#define PID_MAXLEN 10
using namespace v8;
// Go through special routines to become a daemon.
// if successful, returns daemon's PID
Handle<Value> Start(const Arguments& args) {
pid_t pid, sid;
int i, new_fd;
if (args.Length() < 1) {
return ThrowException(Exception::TypeError(
String::New("Must have at least one arg containing the file descriptor")));
new_fd = args[0]->Int32Value();
pid = fork();
if(pid > 0) exit(0);
if(pid < 0) exit(1);
dup2(new_fd, STDOUT_FILENO);
dup2(new_fd, STDERR_FILENO);
sid = setsid();
return Integer::New(getpid());
Handle<Value> SetSid(const Arguments& args) {
pid_t sid;
sid = setsid();
return Integer::New(sid);
extern "C" void init(Handle<Object> target) {
HandleScope scope;
target->Set(String::New("start"), FunctionTemplate::New(Start)->GetFunction());
target->Set(String::New("setSid"), FunctionTemplate::New(SetSid)->GetFunction());
Jump to Line
Something went wrong with that request. Please try again.