A simple way to monitor for any changes in your Node.js application and magically restart the server

computer girl success

In most of my Node.js application servers, I add a simple section of code at the end which watches specific application files, and terminates the server if any are changed.  In tandem with PM2, this allows the code to restart automatically when either pushing a new version, or directly making changes to the code.

There are other applications like nodemon that provide similar functionality, however I feel that PM2 is a better overall process management system.  This capability was not available, so I just added it directly into my applications.

In my application, I perform a simple process.exit(), however you may prefer to raise a signal in your application so you can gracefully shutdown.

console.log('Starting filename watch');

function restartServer(event, filename) {
    console.log('event is: ' + event);
    if (filename) {
        console.log('filename provided: ' + filename);
    } else {
        console.log('filename not provided');

fs.watch('jobs_processor.js', restartServer);
fs.watch('lib/hercle_jobs/lib/hercle_jobs.js', restartServer);
fs.watch('lib/hercle_jobs_v2.js', restartServer);
fs.watch('lib/ims_utility.js', restartServer);

Node.js – fs.open / fs.writeFile, mode, file permissions, umask, and how they interact

Girl Reading Book
While working with Node.js recently, I ran into a situation with the mode parameter used in several functions; fs.open, fs.appendFile, fs.writeFile, etc. When trying to create a new file and specifying a specific mode parameter to allow user and group writes, I found that the file would get created, however would have the wrong permissions.  After much experimenting, I realized that the Linux server’s umask was getting applied, and preventing the creation of a file with the permissions I wanted.

The work around turned out to be fairly simple, although it does create a slight security hole, and should only be used in cases where you can control access to the application.

I simply wrapped the file create calls with the following code :

oldmask = process.umask(newmask);
fs.open(path, flags, [mode], callback)

This only needs be performed when the open (or other method) will create a new file.  Let me know if you have experienced a similar issue, and if you have found other solutions to resolve.

Linux Question of the Day – How do I Grep Recursively?

redheaded lady using laptop

Another common question that I hear on a weekly basis. You would think this would be a pretty straightforward answer, and it is. I think the shear number of options available with Grep is what confuses folks.  So, here is the basic way to perform this task.

grep -r “texthere” .

Simple, right?  “texthere” is the string that you are searching for, and the -r says search recursively starting from the current directory (.).  You can also specify specific filenames or types that you would like to search, such as *.txt, *.php, etc.

On some older Unix versions, you may find that Grep does not support the -r syntax.  In that case, try the following :

find ./ -type f | xargs grep “texthere”

Also som version also will not support searching for *.txt as the filename, in that case, try the following :

find /dir/to/search/ -iname *.txt -exec grep ‘texthere’ ‘{}’ ;

Little known piece of trivia, GREP stands for Get Regular Expression and Print

Using a Windows XServer to access the Gnome desktop on a Linux Server


I wanted to throw a shout-out to a software package called Xming X Server.  I have been using this nifty piece of software to access the Gnome desktop of our corporate Linux servers from the comfort of my Windows based laptop.  Xmind does this my running a XServer under Windows, and using a customized version of Putty to forward all the X packets across a SSH connection.  Even on Internet connection, the performance is very acceptable, and when on a local LAN, it approaches native speed.

A limited version of Xming has been released into the public domain, but throw Mr Colin Harrison £10 and he will send you an unencumbered version.  I am interested to hear from other folks who are using a Windows XServer to connect to a Linux or Mac.