I have an ubuntu server. On which I have transferred some files which have some js and html code.

I used http-server from node.js to start a web server so that I can display the html page on the server.

I used nohup so that it can remain running even if I disconnect or close my system.

Here is the command I used:

nohup http-server -p 8000 -a & 

Now this allows me to visualize the files on but I am noticing after sometime the server goes down and one can't access the html page on that ip in their browser.

I thought nohup is for running things in background even if one closes their system or logs out of server.

So I asked around here and someone suggested using pm2. So got that installed.

As per the suggestion of using pm2, I installed latest version of node and then started service with pm2.

It says service started for http-server but when I go to ip with port 8000 on browser it doesn't open up.

Here is the command I ran in my directory which has the html and d3 files.

$ pm2 start $(which http-server) -p 8000  

And here is the output in shell of the pm2

enter image description here

How to run the server forever without getting halted and what's going wrong with pm2?

  • You should be starting your server as a daemon, the nohup thing is a total red herring, that's just some notion you picked up somewhere that is not related to what you want to achieve. I believe the normal use for nohup is to execute a specific command after logout, that's how I've used it in my programs, it's not intended to run and maintain a process in the manner of a daemon/server that starts on startup and stops on shutdown. – Lizardx Feb 25 '19 at 19:32
  • sure. So I am using pm2 for that. But getting issues with pm2 itself – Baktaawar Feb 25 '19 at 19:48
  • Why are you wasting time with these 3rd party things like pm2, if you use systemd, create a new service, and it will just run. pm2 isn't even in debian, I assume that's some ubuntu thingie. Just do it the real way, start the daemon on boot, stop it on halt, skip all these other things. Search for: how to start service systemd if you use systemd, if it's old ubuntu, upstart, otherwise, sysv if it's really old. – Lizardx Feb 25 '19 at 19:52
  • I understand ur concern. However I am not a devops guy. I am more into ml stuff. I need to run a d3 visualiztion and html file running on an ubuntu server we have access to. So I asked here and ppl suggested use pm2 or Forever. They said it's hassle free and keeps running even when u r logged out of server. I want the ip on the browser to keep running for other to access the viz. If u can spare some time and help me with the steps for the process u r mentioning, wud be glad to learn and follow those. Thnx. – Baktaawar Feb 25 '19 at 19:54
  • 1
    Lol, sorry, no. When you use these non core tools to achieve what are standard actions, then hit a bug, or failure, you simply have one more layer to work through to solve the problem, I don't see how that can be considered 'easier'. Starting a daemon / server is easy, just search for it. If this were actually easier, then it would be working, and you wouldn't be stuck trying to figure out why using the wrong tools for the job created more headaches. So maybe the advice you got wasn't so great? – Lizardx Feb 25 '19 at 19:56

If you don't care about the consequences you just want to do something like this...


while sleep 5; do
    http-server -p 8000 -a

Then name the script, give it execution permission, so you can kill script when you dont want the "server" to keep being restarted. It theorically only restart the server, when it ends.

This will just answer your question but using a systemd unit as suggested in the comments, it is a lot better.

Here some examples of a auto-restarting unit for systemd: https://singlebrook.com/2017/10/23/auto-restart-crashed-service-systemd/

  • Thnx for ans. I care abt the right thing. I am just asking if it is systemed, if someone can help with the steps that wud be appreciable. I am not a devops person, so these things are a bit difficult for me to do all by myself without some steps guidance. Thnx – Baktaawar Feb 25 '19 at 20:14
  • This is the bad about systemd, sometimes it allows you to do some crap thinking you are doing the right thing. If you are really concerned about it, you need to fix the server to do not require auto-restarting anymore. So sorry, can't help anymore, but I think you will find someone that can. – Luciano Andress Martini Feb 25 '19 at 20:17
  • Can u pls highlight the steps to start a web server on ubuntu server running untill stop explicitly? I have been askin this simple ques and everyone is busy giving comments with no sols. If you can help ans I wud appreciate. These comments are fine but don't help me in achieving what I want to get done – Baktaawar Feb 25 '19 at 20:19
  • #systemctl start apache2 ? You mean your web server right? Not a web server – Luciano Andress Martini Feb 25 '19 at 20:22
  • I have node.js installed. I also have http-server installed. How can i run http-server forever. Pls see the ques I added. It tells what I am trying to do – Baktaawar Feb 25 '19 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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