1

I have seen similar questions but none answers mine. It may be that I'm overlooking something.

I have an existing server with subdomain pointing to next.js port and can successfully run nexxtjs app through

npm run dev

I would like to run it each time server restarts so the wite would be constantly up.

I have tried through the systemctl service but I couldn't get it to work.

Is there another way? Am I overlooking something obvious?

2
  • why don't you write a cron job? similar to @reboot cd project_folder && npm run dev May 7, 2019 at 8:26
  • @SirajusSalekin Thaks thats what i actually did May 10, 2019 at 15:14

3 Answers 3

1

@SirajusSalekin actual commented with, what I believe, is the best answer.

Create a cron job to start the application on bootup.

crontab -e

Or to start the application as root:

sudo crontab -eu root

Then add the following:

@reboot cd project_folder && npm run dev
2
  • How do you know it if worked
    – Black
    Aug 4, 2022 at 17:32
  • 1
    I'm not quite sure I understand the question. If you mean how do you know if the cron job was successfully added, you can use crontab -l. If on the other hand you want to know if the server is up and running you could see if the service is listening on the port. For that you could use the lsof command. Hope this helps.
    – habadeer
    Aug 5, 2022 at 22:19
0

The easiest way is to install pm2 and then launch it in pm2. pm2 will automatically relaunch the application with the system boots or even when the application crashes.

sudo npm install pm2 -g
cd myapproot
pm2 start myapp.js
1
  • npm run sometimes have chain of commands. sometimes it's executing nextjs instead of node. May 10, 2019 at 15:13
0

Together put the sudo before the command line, like:

sudo npm start ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .