0

I have a website written in Node.Js that I launch with this command :

cd /var/mywebsite
npm run dev

What should I put in /etc/init.d/rc.local ?

A solution is : https://causeyourestuck.io/2016/04/30/run-node-js-script-startup/ But I don't know how to create the app.js...

Thanks !

FunkycraM
  • 103
  • 1
  • 6

5 Answers5

0

you could use continues integration tools such as puppet to help you do accomplish this. You can also integrate it with Jenkins for the full lifecycle.

https://forge.puppet.com/puppet/nodejs

213374you
  • 226
  • 3
  • 3
0

Given that you are on Linux Ubuntu I think the best and most simple option is a CronTab

#crontab -e
@reboot  /home/user/startServer.sh

then your shell/bash script should look something like this

#!/usr/bin/bash
node server.js

or

#!/user/bin/bash
npm run dev

Let me know if that helps, or if you run into issues. I haven't used linux in a few months :)

more info: How to run a shell script at startup

Patrick Murphy
  • 2,311
  • 14
  • 17
  • I have made the script, made it executable, added it to crontab (as ubuntu user, not root), and it doesn't work... This is the script : #!/bin/bash cd /var/www/mywebsite npm run dev – FunkycraM Oct 23 '17 at 17:29
0

I would recommend a process manager like pm2.

npm i -g pm2
#install pm2

pm2 start <in your project directory>
#starts the project (can start multiple)

pm2 startup
#setup init scripts to start processes on reboot

pm2 save
#saves list of processes

Ref: http://pm2.keymetrics.io/docs/usage/startup/

Brahma Dev
  • 1,955
  • 1
  • 12
  • 18
  • Thanks. "pm2 start" gives me : error: missing required argument `file|json|stdin|app_name|pm_id' What should I put ? I don't have any app.js file – FunkycraM Oct 23 '17 at 17:21
  • It tries to run the `start` script from package.json. You can use `pm2 start somefile.js` if your package.json is not setup that way. – Brahma Dev Oct 23 '17 at 17:24
  • OK, I understand now the problem. My package.json looks like this (I have no .js file mentioned) : "name": "myapp", "scripts": { "dev": "nodemon -w ./server --exec \"babel-node server --presets es2015,stage-0\"", "adduser": "nodemon -w ./server/adduser --exec \"babel-node server/adduser --presets es2015,stage-0\"", "build": "babel server -s -D -d dist --presets es2015,stage-0", "start": "node dist", "prestart": "npm run -s build", "test": "eslint src" }, "engines": { "node": "6.9.4" }, – FunkycraM Oct 23 '17 at 17:37
  • 1
    You need to run `npm run build`. This will generate your js files in `dist` directory. Then you can run `pm2 start dist/index.js`. – Brahma Dev Oct 23 '17 at 17:40
  • OK, thanks a lot for your help. rpm run build works and I have the dist directory. pm2 start dist/index.js works, but pm2 status gives me this status for the app : errored. I thinks that the problem is that when I start the server with "npm run dev" it starts this script (in package.json) : nodemon -w ./server --exec \"babel-node server --presets es2015,stage-0\" – FunkycraM Oct 23 '17 at 17:49
  • There are syntax errors in the script because it is not launched the correct way. Part of the log : /var/www/website/settings.js:8 2|index | export default { 2|index | ^^^^^^ 2|index | SyntaxError: Unexpected token export – FunkycraM Oct 23 '17 at 18:01
  • That's not possible. Run `pm2 flush` then `pm2 delete all` and `pm2 start dist/index.js` – Brahma Dev Oct 23 '17 at 18:03
  • Just tried, but same problem. Log says : App [index] with id [0] and pid [7426], exited with code [1] via signal [SIGINT] ************ Script /var/www/mywebsite/dist/index.js had too many unstable restarts (16). Stopped. "errored" *********** /var/www/website/settings.js:8 2|index | export default { 2|index | ^^^^^^ 2|index | SyntaxError: Unexpected token export – FunkycraM Oct 23 '17 at 18:11
  • I think the problem is that is it launched with nodemon (as written in the package.log) – FunkycraM Oct 23 '17 at 18:12
  • No, all that is completely unrelated. `npm run build` should produce perfectly working code if everything else is configured properly. But it looks like code inside `dist` is trying to access the non transpiled `settings.js` outside the `dist` directory. – Brahma Dev Oct 23 '17 at 18:34
  • If your `settings.js` is not too complicated you can convert it to normal syntax, otherwise you'll have configure babel to transpile that as well. – Brahma Dev Oct 23 '17 at 18:37
0

If you want to use forever-service module, you can use it like this:

Install forever & forever-service packages globally:

sudo npm install -g forever forever-service

Then register your app as a service (default to app.js, but you can specify the name of your app if you need to):

sudo forever-service install your_app --script the_name_of_your_app --start
TGrif
  • 5,725
  • 9
  • 31
  • 52
  • Thanks. The problem is that I don't know the name of my app... I have a server/index.js All I know is that I launch my app with npm run dev – FunkycraM Oct 23 '17 at 17:25
  • So, probably index.js. Check the script dev line in your _package.json_ to be sure. – TGrif Oct 23 '17 at 17:29
  • OK, I understand now the problem. My package.json looks like this (I have no .js file mentioned) : "name": "myapp", "scripts": { "dev": "nodemon -w ./server --exec \"babel-node server --presets es2015,stage-0\"", "adduser": "nodemon -w ./server/adduser --exec \"babel-node server/adduser --presets es2015,stage-0\"", "build": "babel server -s -D -d dist --presets es2015,stage-0", "start": "node dist", "prestart": "npm run -s build", "test": "eslint src" }, "engines": { "node": "6.9.4" }, – FunkycraM Oct 23 '17 at 17:38
  • Give it a try with ./server, otherwise ./server/index.js. And I think you'll need to pass others command line parameters with --scriptOptions argument. (Or build it before as Brahma Dev suggests) – TGrif Oct 23 '17 at 17:47
0

I have understood the problem and founded the solution !! A big thank to @Brahma Dev and @TGrif for their help !

The problem was that on the server I have node.js install globally (via apt-get) and an other version installed a the user which launch npm run dev

And if I execute su myuser -c "node -v, su doesn't execute .bashrc before node -v. So if I execute su myuser and then node -v, the version of node is different !

I have solved the problem by creating an sh script containing : export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion npm run dev

And now, su myuser -c "./start-server.sh" works !

So if I add su myuser -c "/var/www/dialoguea.co.tools/dialoguea/start-server.sh" & in /etc/rc.local the node.js server loads when ubuntu is restarted.

FunkycraM
  • 103
  • 1
  • 6