Managing Nodejs servers with foreverjs


Foreverjs is not only a simple CLI tool for ensuring that a given script runs continuously, but also has advanced features support running multiple nodejs applications. By using the -w flag, it gives you the option of restarting the server on file changes and we don’t only automatically restart the application when the code changes, but also when the nodejs application goes down.

Installation:

npm install -g forever

Result for the installation on windows:

C:\Users\Administrator\AppData\Roaming\npm\forever -> C:\Users\Administrator\App
Data\Roaming\npm\node_modules\forever\bin\forever
forever@0.15.1 C:\Users\Administrator\AppData\Roaming\npm\node_modules\forever
├── path-is-absolute@1.0.0
├── object-assign@3.0.0
├── colors@0.6.2
├── clone@1.0.2
├── timespan@2.3.0
├── optimist@0.6.1 (wordwrap@0.0.3, minimist@0.0.10)
├── prettyjson@1.1.3 (colors@1.1.2, minimist@1.2.0)
├── nssocket@0.5.3 (eventemitter2@0.4.14, lazy@1.0.11)
├── winston@0.8.3 (cycle@1.0.3, stack-trace@0.0.9, eyes@0.1.8, isstream@0.1.2, a
sync@0.2.10, pkginfo@0.3.0)
├── cliff@0.1.10 (eyes@0.1.8, colors@1.0.3)
├── nconf@0.6.9 (ini@1.3.4, async@0.2.9, optimist@0.6.0)
├── shush@1.0.0 (strip-json-comments@0.1.3, caller@0.0.1)
├── utile@0.2.1 (deep-equal@1.0.1, ncp@0.4.2, i@0.3.3, async@0.2.10, mkdirp@0.5.
1, rimraf@2.4.3)
├── flatiron@0.4.3 (optimist@0.6.0, director@1.2.7, broadway@0.3.6, prompt@0.2.1
4)
└── forever-monitor@1.6.0 (broadway@0.3.6, ps-tree@0.0.3, minimatch@2.0.10, chok
idar@1.0.5)

foreverjs install

Read more: How to install and use ForeverJS on CentOS

Usage:

forever [action] [options] SCRIPT [script-options]
actions:
    start               Start SCRIPT as a daemon
    stop                Stop the daemon SCRIPT by Id|Uid|Pid|Index|Script
    stopall             Stop all running forever scripts
    restart             Restart the daemon SCRIPT
    restartall          Restart all running forever scripts
    list                List all running forever scripts
    config              Lists all forever user configuration
    set <key> <val>     Sets the specified forever config <key>
    clear <key>         Clears the specified forever config <key>
    logs                Lists log files for all forever processes
    logs <script|index> Tails the logs for <script|index>
    columns add <col>   Adds the specified column to the output in `forever list`
    columns rm <col>    Removed the specified column from the output in `forever list`
    columns set <cols>  Set all columns for the output in `forever list`
    cleanlogs           [CAREFUL] Deletes all historical forever log files

  options:
    -m  MAX          Only run the specified script MAX times
    -l  LOGFILE      Logs the forever output to LOGFILE
    -o  OUTFILE      Logs stdout from child script to OUTFILE
    -e  ERRFILE      Logs stderr from child script to ERRFILE
    -p  PATH         Base path for all forever related files (pid files, etc.)
    -c  COMMAND      COMMAND to execute (defaults to node)
    -a, --append     Append logs
    -f, --fifo       Stream logs to stdout
    -n, --number     Number of log lines to print
    --pidFile        The pid file
    --uid            Process uid, useful as a namespace for processes (must wrap in a string)
                     e.g. forever start --uid "production" app.js
                         forever stop production
    --sourceDir      The source directory for which SCRIPT is relative to
    --workingDir     The working directory in which SCRIPT will execute
    --minUptime      Minimum uptime (millis) for a script to not be considered "spinning"
    --spinSleepTime  Time to wait (millis) between launches of a spinning script.
    --colors         --no-colors will disable output coloring
    --plain          Disable command line colors
    -d, --debug      Forces forever to log debug output
    -v, --verbose    Turns on the verbose messages from Forever
    -s, --silent     Run the child script silencing stdout and stderr
    -w, --watch      Watch for file changes
    --watchDirectory Top-level directory to watch from
    --watchIgnore    To ignore pattern when watch is enabled (multiple option is allowed)
    --killSignal     Support exit signal customization (default is SIGKILL),
                     used for restarting script gracefully e.g. --killSignal=SIGTERM
    -h, --help       You're staring at it

Some simple usage:

forever start -w server.js

Result:

warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up f
or at least 1000ms
error: Could not read .foreverignore file.
error: ENOENT, open 'D:\AppServ\www\chat3\.foreverignore'
Listening to localhost:8080 ...

Some error occur but no problem.

By using the -w flag, if you change your code, the node will automatically restart:

error: restarting script because change changed
error: Script restart attempt #1
Listening to localhost:8080 ...

To list all running forever scripts, use this command:

forever list

Result:

info:    Forever processes running
data:        uid  command                                  script
          forever pid   id logfile                                  uptime
data:    [0] Wlf4 "C:\Program Files (x86)\nodejs\node.exe" D:\AppServ\www\chat3\
server.js 25944   25728    C:\Users\Administrator\.forever\Wlf4.log STOPPED

forever list

We can use json to configuration one or many application.
File app.json

{
    "uid": "app1",
    "append": true,
    "watch": true,
    "script": "server.js",
    "sourceDir": "D:/Appserv/www/chat3" 
}

Run this command:

forever "D:\Appserv\www\node\test\app.json"

(We can use absolute path or relative path to json file.)

Result:

C:\Users\Administrator>forever "D:\Appserv\www\node\test\app.json"
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up f
or at least 1000ms
error: Could not read .foreverignore file.
error: ENOENT, open 'D:\Appserv\www\chat3\.foreverignore'
Listening to localhost:8080 ...

To run many applications, you use the json file: ex: app.json

[
  {
    "uid": "app1",
    "append": true,
    "watch": true,
    "script": "server.js",
    "sourceDir": "D:/Appserv/www/chat3" 
  },
  {
    "uid": "app2",
    "append": true,
    "watch": true,
    "script": "app.js",
    "sourceDir": "D:/Appserv/www/chat" 
  }
]
C:\Users\Administrator>forever list
info:    Forever processes running
data:        uid  command                                  script
          forever pid   id logfile                                  uptime

data:    [0] app1 "C:\Program Files (x86)\nodejs\node.exe" D:\Appserv\www\chat3\
server.js 23672   2080     C:\Users\Administrator\.forever\app1.log STOPPED

data:    [1] app2 "C:\Program Files (x86)\nodejs\node.exe" D:\Appserv\www\chat\a
pp.js     23672   24372    C:\Users\Administrator\.forever\app2.log 0:0:5:18.591

To stop all running forever scripts:

forever stopall

Result:

C:\Users\Administrator>forever stopall
info:    No forever processes running
info:    Forever stopped processes:
data:        uid  command                                  script
          forever pid   id logfile                                  uptime

data:    [0] app1 "C:\Program Files (x86)\nodejs\node.exe" D:\Appserv\www\chat3\
server.js 23672   2080     C:\Users\Administrator\.forever\app1.log STOPPED

data:    [1] app2 "C:\Program Files (x86)\nodejs\node.exe" D:\Appserv\www\chat\a
pp.js     23672   24372    C:\Users\Administrator\.forever\app2.log 0:0:13:28.23
7

Restart running forever scripts. We have 2 cases of usage.

1. Stop a specific script: by uid or path of script.

forever restart D:\Appserv\www\chat3\server.js

or

forever restart app1

2. Stop all running forever scripts.

forever restartall

Wow, clearly, ForeverJs is the best CLI tool for run multiple nodejs applications.

1 Comment

Leave a Reply