Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

I'm sure this is a really stupid question and it's probably already been asked a dozen times on this site, and I just don't know which keywords to search. I'm still really new to managing a server and new to the command line in general, but it's something I've been wanting to try for a long time.

Right now I have Ubuntu Server (no GUI) running a Node.js web server.

How can I keep Node running while executing other commands on the command line?

For example:

node /var/www/my_app.js

This command prevents me from running other commands until I terminate it with Ctrl + C. But what if I want to run a redis-cli command while leaving Node running in the background? In other words:

  1. How can I exit out of the Node.js context without stopping the Node server? and
  2. After I do (1), how can I return to the Node context?

I don't know if (2) makes sense for the node command, but, for example, if I ran nodemon instead, I can run a rs (restart) command while nodemon is running.

Thanks, and apologies if this is a duplicate and/or really obvious question.

share|improve this question

marked as duplicate by Lucio, terdon, guntbert, Eric Carvalho, Radu Rădeanu May 10 at 16:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 3 down vote accepted

The suggested (setsid, et al) are great if you're just firing something off and don't care about its longevity but in production you'd use a proper daemon system to monitor and respawn a server process. Ubuntu ships with Upstart so I'd look at writing an Upstart script.

Save something like this to somewhere like /etc/init/mynodeapp.conf:

description "My node.js app"

start on startup
stop on shutdown
respawn

exec sudo -u USER /usr/bin/node /var/www/my_app.js

Replace USER in the exec line with whatever user you want to be running, and make sure the path to the node binary is full and correct (which node will help confirm). If you're using a locally installed version, make sure it points there.

Once that's in order, sudo start mynodeapp will start it, sudo stop mynodeapp will stop it and various other upstart commands (restart, status, etc) will do whatever they are designed to do. If you don't want it to start on boot, just comment out the start line with a #.


That's how I'd do it in production.... You mention wanting to "return to the Node context"... That's a bit harder. Upstart and even the other process reparenting techniques all detach you from the process. There's usually no going back to the lit firework.

If you have an ongoing process that you need to tend to, it's probably your terminal session you want to look at changing. If I really needed what you're suggesting, I'd start looking at screen. This allows you to have multiple hanging terminals that you can detatch and reattach from and to respectively.

It's not a simple application to master but one that might be worth digging into:

share|improve this answer
    
Thanks! I ended up installing screen, which is really handy. I love being able to just switch between multiple terminals when making commands. –  M Miller May 10 at 18:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.