Serve: How to serve in the background as a demon.

Created on 26 Dec 2017  路  9Comments  路  Source: vercel/serve

Hi I want to use serve for production however when ever I run it using
serve -s build
it is killed whenever I close the terminal or press ctrl-c.

Most helpful comment

@burtontanner That looks like it should be an npm start script:

// package.json
{
  "scripts": {
    "start": "serve . --single -p 4000"
  }
}

And then you can use pm2 to run npm scripts:

pm2 start npm -- start

All 9 comments

I'm pretty sure that there are better ways than this but you can run it as a background process (see here information: https://kb.iu.edu/d/afnz):

serve -s build &

You also might want to redirect stdout & stderr into a separate file for logging.

@sarfrazarshad You can use something like screen or tmux to keep the session open. Also, take a look at pm2, you can do a lot of things with it.

鈽濓笍 Exactly.

Or create a Node.js child process to fork it into the background. But this is definitely not something that serve needs to support - i hope you understand.

@pablopunk do you know how I could run "serve . --single -p 4000" but with pm2?

@burtontanner what's the problem with that?

@pablopunk I was wanting to do something like "pm2 start 'serve --single -p 4000'". I ended up creating a bash script (serve.sh) that contained 'serve . --single -p 4000'. Then I could run 'pm2 start serve.sh'.

@pablopunk I was trying to run something like 'pm2 start "serve . --single -p 4000"'. I ended up creating a bash script (serve.sh), and putting 'serve . --single -p 4000' inside it. Then I ran pm2 with 'pm2 start serve.sh'

@burtontanner That looks like it should be an npm start script:

// package.json
{
  "scripts": {
    "start": "serve . --single -p 4000"
  }
}

And then you can use pm2 to run npm scripts:

pm2 start npm -- start

Ahh I didn't think of that. Thanks @pablopunk

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arel picture arel  路  4Comments

gaearon picture gaearon  路  4Comments

Simran-B picture Simran-B  路  4Comments

leo picture leo  路  5Comments

leo picture leo  路  5Comments