Keep Node running forever

If you’ve ever started up a deployed Node application, you’ve probably had this problem: you start the server with node server.js, check on your site, and it’s up and running. Great! Some time later, your SSH session times out or you close your terminal. Then you go to your site, and it’s down. What happened?

You ran the server in the foreground, which means that when you log out of SSH, it will be stopped along with all the other running processes.

I’ve found two good solutions. Before using either of them, make sure you are starting the server with root privileges by running sudo -i. This will take you to the root of the server, so you will need to cd back to the folder where the server file is located.

Method 1: Run node in the background

In this method, you start Node with the & flag to run it in the background and the nohup flag to detach it from the console window.

To start the Node server:

sudo nohup node server.js &

To shut down the Node server, first find the running process ID with:

ps -aef | grep node
// root     21404     1  0 08:05 ?        00:00:00 node server.js
// alex     21530 21494  0 08:08 pts/0    00:00:00 grep --color=auto node

Then kill the process via the ID:

sudo kill 21404

More details are in these Stack Overflow posts: run node permanently and stop node.

Method 2: Use Forever to keep the server up

In this method, you install Forever and use it to run the server for you. This method is more robust, because it will restart your server automatically if it crashes.

Make sure to use forever start to run the server as a service.

sudo npm install -g forever
sudo forever start server.js

Now, when you close your terminal window, Node will continue to run. Here are a few helpful forever commands from the StackOverflow post:

To list all running processes:

forever list

Note the integer in the brackets and use it as following to stop a process:

sudo forever stop 0

Restarting a running process goes:

sudo forever restart 0

If you’re working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes:

sudo forever -w server.js

More info on Forever is available on GitHub.

Meetup notes: Microsoft Bot Framework

Last night, I went to a meetup at Slack on the Microsoft Bot Framework. It was a good opportunity to get a grasp on what the Bot Framework is, and how developers are using it to create bots that deploy on several different channels, including (as of March 2016):

  • Text/SMS
  • Office 365 mail
  • Skype
  • Slack
  • GroupMe
  • Telegram
  • Facebook Messenger
  • Kik
  • Web (via the Bot Framework embeddable web chat control).

The general idea is that you can write a bot in the Microsoft Bot Framework, and it will be portable to all these channels, with some slight modifications depending on the specifics. Great! But what is a bot, anyway?

Generally, a bot embodies some kind of AI, or artificial intelligence. It’s an app that acts like a person, and it helps you accomplish work by using contextual meaning. If you’re an iPhone user, you’re familiar with Siri. Siri is essentially a bot – it asks you questions and accomplishes some kind of work by reasoning about your answers. Here’s a screenshot from the Bot Framework splash page:

Screen Shot 2016-06-23 at 1.28.01 PM

Mat Velloso gave the presentation, and one example he gave is called Murphy. You ask Murphy any kind of ‘what if’ question, and it goes out and finds images that match your scenario, then mashes them together. Basically, it’s face swap via bot. You can say ‘what if I were the president?’ and Murphy will probably give you an image of President Obama with your face. This is a rudimentary example, but you can see how bots would make lots of things faster, especially on a mobile device. You can give short, partial answers and depend on the natural language processor to figure out what you mean.

The SDK, available on GitHub, is written in C# and Node.js. You can fork the repo and start working from examples to build your own bots. Here are a few more resources that were covered during the presentation:

docs.botframework.com – take a look at the docs

luis.ai – lets your app understand language

microsoft.com/cognitive-services – a whole suite of services like LUIS, including face and voice recognition, academic knowledge, image search, etc.

bot framework emulator – lets you play with your bot as your develop it. Includes a GUI for Windows and a command-line version for Mac / Linxu.

 

Looking forward to playing with this framework and maybe building a bot of my own in the near future!

Downgrading a DigitalOcean Droplet with ServerPilot & WordPress

If you are a WordPress admin looking for a way to downgrade your DigitalOcean droplet, you probably came across their article How To Downgrade DigitalOcean Droplets, which tells you to use rsync to move files from your old droplet to the new droplet. If you are using using ServerPilot to manage your DigitalOcean server, do not follow these instructions. ServerPilot has an article called Cloning and Resizing Servers that explicitly tells you not to use rsync. There are a few problems with rsync – basically, it will not correctly clone your ServerPilot configuration, so your new site will not work. Here’s a more automated method using a WordPress plugin called ManageWP, which I found to be a better solution:

  1. Create a new server on Digital Ocean – do this using the smaller subscription that you want to downgrade to.
  2. Connect the new server to Server Pilot
  3. Install WordPress with one click on the new server
  4. Install Manage WP on both sites (you need to install the plugin under the same account in both sites – just do the sections titled Install the ManageWP Worker Plugin and Set Up ManageWP).
  5. Migrate WordPress with Manage WP 
  6. Go to the new IP address in a browser and verify that it works
  7. Update DNS records with GoDaddy to point to the new site

If you found this useful or have any feedback on this process, leave a comment!