Giter VIP home page Giter VIP logo

node-startup's Introduction

node-startup

Startup script for Linux-based systems for running a Node.js app when rebooting, using an /etc/init.d script.

Looking for a maintainer

If you use node-startup and would like to be a maintainer, send me a message.

Installation

Clone the repo:

git clone https://github.com/chovy/node-startup.git
cd node-startup/init.d

Edit the node-app script with your settings from the Configuration section, then follow instructions in the Running section.

Configuration

At the top of the node-app file, a few items are declared which are either passed to the Node.js app or used for general execution/management.

Node.js Config

The items declared and passed to the Node.js application are:

  • NODE_ENV - the type of environment - development, production, etc. - can be read by the application to do things conditionally (defaults to "production")
  • PORT - the port that the Node.js application should listen on - should be read by the application and used when starting its server (defaults to "3000")
  • CONFIG_DIR - used for node-config (defaults to "$APP_DIR"); is required, but should be kept as the default if not needed

Execution Config

The items declared and used by the overall management of executing the application are:

  • NODE_EXEC - location of the Node.js package executable - useful to set if the executable isn't on your PATH or isn't a service (defaults to $(which node))
  • APP_DIR - location of the Node.js application directory (defaults to "/var/www/example.com")
  • NODE_APP - filename of the Node.js application (defaults to "app.js")
  • PID_DIR - location of the PID directory (defaults to "$APP_DIR/pid")
  • PID_FILE - name of the PID file (defaults to "$PID_DIR/app.pid")
  • LOG_DIR - location of the log (Node.js application output) directory (defaults to "$APP_DIR/log")
  • LOG_FILE - name of the log file (defaults to "$LOG_DIR/app.log")
  • APP_NAME - name of the app for display and messaging purposes (defaults to "Node app")

Available Actions

The service exposes 4 actions:

  • start - starts the Node.js application
  • stop - stops the Node.js application
  • restart - stops the Node.js application, then starts the Node.js application
  • status - returns the current running status of the Node.js application (based on the PID file and running processes)

Testing

Test that it all works:

/etc/init.d/node-app start
/etc/init.d/node-app status
/etc/init.d/node-app restart
/etc/init.d/node-app stop

Add node-app to the default runlevels:

# Debian
update-rc.d node-app defaults
# RHEL, --del to remove
chkconfig --add node-app

Finally, reboot to be sure the Node.js application starts automatically:

Force Action## Why node-startup?

When my VPS was rebooted occassionally by the hosting provider, my Node.js app was not coming back online after boot. This script can be used in /etc/init.d, which will allow rc.d to restart your app when the machine reboots without your knowledge.

If you are using MongoDB, Redis, or Nginx, you want to add those to your default run-level as well.

Running

Copy the startup script node-app to your /etc/init.d directory:

sudo bash -l
cp ./init.d/node-app /etc/init.d/

In addition to the start, stop, and restart actions, a --force option can be added to the execution so that the following scenarios have the following outcomes:

  • start - PID file exists but application is stopped -> removes PID file and starts the application

  • stop - PID file exists but application is stopped -> removes PID file

  • restart - either of the above scenarios occur

    sudo reboot

Supported OS

Tested with Debian 6.0, but it should work on other Linux systems that use startup scripts in /etc/init.d (Red Hat, CentOS, Gentoo, Ubuntu, etc.).

LICENSE

(The MIT License)

Bitdeli Badge

node-startup's People

Contributors

anthonyettinger avatar azariah001 avatar bitdeli-chef avatar bryant1410 avatar chovy avatar darul75 avatar diegoalejogm avatar ematthews avatar ianpgall avatar joawan avatar jstuckey avatar kerber avatar marcoamorales avatar praharsh-patel avatar ralyodio avatar rom3r4 avatar stephentrombetti avatar thetrompf avatar wheller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-startup's Issues

The application is aborted when I open it in the browser

OS: Ubuntu 14.04 LTS

When I open the application in the browser, the html file is loaded. But all others can't not be loaded because the node has been stopped.

app.log file:

/home/alexchao/www/jobcn-deploy-tool/server/index.js: 1: /home/alexchao/www/jobcn-deploy-tool/server/index.js: Syntax error: "(" unexpected

index.js file:

var fs = require('fs');
var restify= require('restify');
// ...

There is another problem that the log and pid directories require root account. When I use a normal account to login, the app will stop because of permission.

stdin: is not a tty

I am getting the following when attempting to run: /etc/init.d/node-app start

Starting node-app ...
stdin: is not a tty
node-app started with pid 16638

but it does not start (can start manually no problem).

Can't running node app when rebooting

I follwed the instruction, now I can use service node-app to start | stop, But when I reboot the host, node-app will not running automatically, "update-rc.d node-app defaults" is ok .. OS: ubuntu 14.04,

Change user

The script should change the user when executed as root.

add option to open log file in "append" mode

It would be nice to have an option to open the log file in "append" mode (">>") rather than "open" mode (">") for the following reasons:

  • for some applications it is desirable to keep the log file when the service is restarted. With the "open" mode the log file is overwritten each time when the service is started/restarted.
  • using the append mode allows for simple log-rotation in "copytruncate" mode. While "copytruncate" does not always fit as you may loose some bytes of log-data during rotate, it comes handy if you don't want to restart the service after log rotation. The latter seems to be the only choice with node-startup as it does not support handling for the "HUP" signal to reopen the log file (which would be tricky anyway).

node app does not stop

command node-app stop answer that app stops and node-app status also say is stopped

but nodebb is running, is there any way to fix it?

Positive Feedback

@chovy Just wanted to say I really dig this startup script. I was able to get my node app up and running as a init.d service in a matter of minutes, and I really appreciate the ease-of-use and thoroughness of your README. I used github to star this repo, but I wish there were a better way to show support and appreciation for good open source projects like this. My workaround is to create an issue that you can just quickly and easily close.

;)

Readme typo

Shouldn't the line
update-rd.c node-app defaults
Read
update-rc.d node-app defaults

cannot restart from admin panel

restart from Admin Panel is not possible when app is running from "node-app" script

[meta.restart] Could not restart, are you sure NodeBB was started with `./nodebb start`?

HOST?

I am running on a VPS, and have a 2nd IP address that all the Node stuff runs on on port 80, is it possible to add this option to the script?

I.e. Host 209.122.xx.22
port 80

Run restart from started node app?

There doesn't seem to be a way to restart the script within node...

I can successfully kill the running app, but it won't start up again. I've tried nohup and exec

nohup makes no difference compared to running restart directly. Exec yields sudo: exec: command not found

Has anyone figured this out? I'm trying to build a webhook handler that updates the webhook handler with a git pull and app restart.

const { exec } = require('child_process');

function handler(req, res, next) {
  exec('sudo exec /etc/init.d/web-hooks restart --force', function (err, stdout, stderr) {
    console.log(err, stdout, stderr)
  })
}

Script doesn't start Node

root@HomeKitPi:~# /etc/init.d/node-app start
Node app stopped, but pid file exists

I looked at issue #17, and I am using the latest version which includes the change to line 45, but for some reason it still doesn't work

Can't support multiple node service

When trying to add multiple node services , running update-rc.d <app_name> defaults gives:

insserv: script <app_name>: service node already provided! insserv: exiting now! update-rc.d: error: insserv rejected the script header

is there a way to get around this?

Unable to start

Hi,
I tried to use your app, without success. It should be a matter of configuration I cannot get rid of.

I have a react application whose structure is as follow:

/src
/src/App.js
/src/index.js

The index.js file is as follow:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';
import 'bootstrap/dist/css/bootstrap.min.css';
import { MemoryRouter } from 'react-router';

ReactDOM.render(
  <MemoryRouter>
    <App />
  </MemoryRouter>,
  document.getElementById('root')
);

The package.json is as follow:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.12.3",
    "@material-ui/icons": "^4.11.2",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "axios": "^0.21.1",
    "bootstrap": "^5.1.0",
    "react": "^17.0.2",
    "react-bootstrap": "^2.0.0-beta.4",
    "react-dom": "^17.0.2",
    "react-icons": "^4.2.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "^4.0.3",
    "react-table": "^7.7.0",
    "web-vitals": "^1.0.1",
    "underscore": "^1.9.2"

  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
...

Usually, I start the application with npm start and it runs fine.

I config your app as follow:

APP_NAME="app"
USER="ubuntu"
GROUP="$USER"
NODE_ENV="development"
PORT="3000"
APP_DIR="/home/ubuntu/KebraContest"
NODE_APP="./src/index.js"
KWARGS=""
CONFIG_DIR="$APP_DIR"
PID_DIR="$APP_DIR/pid"
PID_FILE="$PID_DIR/$APP_NAME.pid"
LOG_DIR="$APP_DIR/log"
LOG_FILE="$LOG_DIR/$APP_NAME.log"
NODE_EXEC=$(which node)

I need to start with sudo, otherwise it doesn't run. This is the result:

sudo /etc/init.d/node-app start
Starting app ...
app started with pid 1096

The application doesn't respond on port 3000, and

sudo /etc/init.d/node-app status
app stopped, but pid file exists

the log reports:

cat log/app.log 
(node:1096) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/ubuntu/KebraContest/src/index.js:1
import React from 'react';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:988:16)
    at Module._compile (internal/modules/cjs/loader.js:1036:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

And this sound strange to me, because the app runs nicely when started manually.

Any suggestion, please?

Thank you

PID recorded on AWS EC2 at startup was wrong.

One more issue:
the PID recorded for the node-startup app is pointing to the bash shell used to start the script instead of referencing the node app that is actually running.

why would this happen? And how can I fix it?

`is_running` detection implementation

Wouldn't it better to leverage the /proc filesystem to figure out if the process is running instead of "parsing" ps output with awk and grep?

is_running() {
    [ -d "/proc/$(get_pid)" ] 
}

I'm getting the wrong pid in AWS linux

Hi,

I was wondering if you could help me out but I'm using your script but the generate pid always seems to get 2 number below the real process id of the running node app.

i.e. the reported pid is 20195 but when I tried to do a ps -aux | grep node, i could see the pid is actually 20197.

Do you know what might be going wrong

Using node-startup in AWS EC2 - reboot does not start the service

node-startup is real simple and cool. I tried to use it for setting up an app on AWS EC2 instance. Initially I had a few failures from
a) Needing to add "sudo" to all the commands including "rm pid file", "mkdir", "chown", etc. Why would this be required? ... this was required when I was running this from the bash shell directly to test out the script.
b) Even after setting this script up in /etc/init.d and setting up the chkconfig --add node-startup
, I noticed that the app does not start up after a reboot automatically. What might be wrong?
c) One of the reason that at reboot the node-startup does not work is, the old pid file is still around. Hence the start command fails. How can i ensure that the node-startup app is shutdown or pid file is ignored only at the reboot and initial startup?

Thanks for your help. Again, this script is nice and simple!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.