UNPKG

12.3 kBMarkdownView Raw
1# forever
2
3[![Join the chat at https://gitter.im/foreverjs/forever](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/foreverjs/forever?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4
5[![Version npm](https://img.shields.io/npm/v/forever.svg?style=flat-square)](https://www.npmjs.com/package/forever)[![npm Downloads](https://img.shields.io/npm/dm/forever.svg?style=flat-square)](https://www.npmjs.com/package/forever)[![Build Status](https://img.shields.io/travis/foreverjs/forever/master.svg?style=flat-square)](https://travis-ci.org/foreverjs/forever)[![Dependencies](https://img.shields.io/david/foreverjs/forever.svg?style=flat-square)](https://david-dm.org/foreverjs/forever)[![Inline docs](http://inch-ci.org/github/foreverjs/forever.svg?branch=master)](http://inch-ci.org/github/foreverjs/forever)
6
7[![NPM](https://nodei.co/npm/forever.png?downloads=true&downloadRank=true)](https://nodei.co/npm/forever/)
8
9
10A simple CLI tool for ensuring that a given script runs continuously (i.e. forever).
11
12## Installation
13
14``` bash
15 $ [sudo] npm install forever -g
16```
17
18**Note:** If you are using forever _programmatically_ you should install [forever-monitor][0].
19
20``` bash
21 $ cd /path/to/your/project
22 $ [sudo] npm install forever-monitor
23```
24
25## Usage
26There are two ways to use forever: through the command line or by using forever in your code. **Note:** If you are using forever _programatically_ you should install [forever-monitor][0].
27
28### Command Line Usage
29You can use forever to run scripts continuously (whether it is written in node.js or not).
30
31**Example**
32```
33forever start app.js
34```
35
36**Options**
37```
38 $ forever --help
39 usage: forever [action] [options] SCRIPT [script-options]
40
41 Monitors the script specified in the current process or as a daemon
42
43 actions:
44 start Start SCRIPT as a daemon
45 stop Stop the daemon SCRIPT by Id|Uid|Pid|Index|Script
46 stopall Stop all running forever scripts
47 restart Restart the daemon SCRIPT
48 restartall Restart all running forever scripts
49 list List all running forever scripts
50 config Lists all forever user configuration
51 set <key> <val> Sets the specified forever config <key>
52 clear <key> Clears the specified forever config <key>
53 logs Lists log files for all forever processes
54 logs <script|index> Tails the logs for <script|index>
55 columns add <col> Adds the specified column to the output in `forever list`. Supported columns: 'uid', 'command', 'script', 'forever', 'pid', 'id', 'logfile', 'uptime'
56 columns rm <col> Removed the specified column from the output in `forever list`
57 columns set <cols> Set all columns for the output in `forever list`
58 cleanlogs [CAREFUL] Deletes all historical forever log files
59
60 options:
61 -m MAX Only run the specified script MAX times
62 -l LOGFILE Logs the forever output to LOGFILE
63 -o OUTFILE Logs stdout from child script to OUTFILE
64 -e ERRFILE Logs stderr from child script to ERRFILE
65 -p PATH Base path for all forever related files (pid files, etc.)
66 -c COMMAND COMMAND to execute (defaults to node)
67 -a, --append Append logs
68 -f, --fifo Stream logs to stdout
69 -n, --number Number of log lines to print
70 --pidFile The pid file
71 --uid DEPRECATED. Process uid, useful as a namespace for processes (must wrap in a string)
72 e.g. forever start --uid "production" app.js
73 forever stop production
74 --id DEPRECATED. Process id, similar to uid, useful as a namespace for processes (must wrap in a string)
75 e.g. forever start --id "test" app.js
76 forever stop test
77 --sourceDir The source directory for which SCRIPT is relative to
78 --workingDir The working directory in which SCRIPT will execute
79 --minUptime Minimum uptime (millis) for a script to not be considered "spinning"
80 --spinSleepTime Time to wait (millis) between launches of a spinning script.
81 --colors --no-colors will disable output coloring
82 --plain Disable command line colors
83 -d, --debug Forces forever to log debug output
84 -v, --verbose Turns on the verbose messages from Forever
85 -s, --silent Run the child script silencing stdout and stderr
86 -w, --watch Watch for file changes
87 --watchDirectory Top-level directory to watch from
88 --watchIgnore To ignore pattern when watch is enabled (multiple option is allowed)
89 -t, --killTree Kills the entire child process tree on `stop`
90 --killSignal Support exit signal customization (default is SIGKILL),
91 used for restarting script gracefully e.g. --killSignal=SIGTERM
92 Any console output generated after calling `forever stop/stopall` will not appear in the logs
93 -h, --help You're staring at it
94
95 [Long Running Process]
96 The forever process will continue to run outputting log messages to the console.
97 ex. forever -o out.log -e err.log my-script.js
98
99 [Daemon]
100 The forever process will run as a daemon which will make the target process start
101 in the background. This is extremely useful for remote starting simple node.js scripts
102 without using nohup. It is recommended to run start with -o -l, & -e.
103 ex. forever start -l forever.log -o out.log -e err.log my-daemon.js
104 forever stop my-daemon.js
105```
106
107There are [several examples][1] designed to test the fault tolerance of forever. Here's a simple usage example:
108
109``` bash
110 $ forever -m 5 examples/error-on-timer.js
111```
112
113### JSON Configuration Files
114
115In addition to passing forever the path to a script (along with accompanying options, described above), you may also pass forever the path to a JSON file containing these options. For example, consider an application with the following file structure:
116
117```
118.
119├── forever
120│ └── development.json
121└── index.js
122
123// forever/development.json
124{
125 // Comments are supported
126 "uid": "app",
127 "append": true,
128 "watch": true,
129 "script": "index.js",
130 "sourceDir": "/home/myuser/app",
131 "logFile": "/home/myuser/logs/forever.log",
132 "outFile": "/home/myuser/logs/out.log",
133 "errFile": "/home/myuser/logs/error.log"
134}
135```
136
137This application could be started with forever, as shown below:
138
139``` bash
140$ forever start ./forever/development.json
141```
142
143Absolute paths to such configuration files are also supported:
144
145``` bash
146$ forever start /home/myuser/app/forever/development.json
147```
148
149**Note:** Forever parses JSON configuration files using [shush](https://github.com/krakenjs/shush), allowing the use of in-line comments within such files.
150
151#### Multi-App Configuration Files
152
153JSON configuration files can also be used to define the startup options for *multiple* applications, as shown below.
154
155```
156[
157 {
158 // App1
159 "uid": "app1",
160 "append": true,
161 "watch": true,
162 "script": "index.js",
163 "sourceDir": "/home/myuser/app1"
164 },
165 {
166 // App2
167 "uid": "app2",
168 "append": true,
169 "watch": true,
170 "script": "index.js",
171 "sourceDir": "/home/myuser/app2",
172 "args": ["--port", "8081"]
173 }
174]
175```
176
177### Using In Your Code
178The forever module exposes some useful methods to use in your code. Each method returns an instance of an EventEmitter which emits when complete. See the [forever cli commands][2] for sample usage.
179
180**Remark:** As of `forever@0.6.0` processes will not automatically be available in `forever.list()`. In order to get your processes into `forever.list()` or `forever list` you must instantiate the `forever` socket server:
181
182``` js
183 forever.startServer(child);
184```
185
186This method takes multiple `forever.Monitor` instances which are defined in the `forever-monitor` dependency.
187
188#### forever.load (config)
189_Synchronously_ sets the specified configuration (config) for the forever module. There are two important options:
190
191Option | Description   | Default
192------- | ------------------------------------------------- | ---------
193root | Directory to put all default forever log files | `forever.root`
194pidPath | Directory to put all forever *.pid files | `[root]/pids`
195sockPath | Directory for sockets for IPC between workers | `[root]/sock`
196loglength | Number of logs to return in `forever tail` | 100
197columns | Array of columns to display when `format` is true | `forever.config.get('columns')`
198debug | Boolean value indicating to run in debug mode | false
199stream | Boolean value indicating if logs will be streamed | false
200
201#### forever.start (file, options)
202Starts a script with forever. The `options` object is what is expected by the `Monitor` of `forever-monitor`.
203
204#### forever.startDaemon (file, options)
205Starts a script with forever as a daemon. WARNING: Will daemonize the current process. The `options` object is what is expected by the `Monitor` of `forever-monitor`.
206
207#### forever.stop (index)
208Stops the forever daemon script at the specified index. These indices are the same as those returned by forever.list(). This method returns an EventEmitter that raises the 'stop' event when complete.
209
210#### forever.stopAll (format)
211Stops all forever scripts currently running. This method returns an EventEmitter that raises the 'stopAll' event when complete.
212
213The `format` parameter is a boolean value indicating whether the returned values should be formatted according to the configured columns which can set with `forever columns` or programmatically `forever.config.set('columns')`.
214
215#### forever.list (format, callback)
216Returns a list of metadata objects about each process that is being run using forever. This method will return the list of metadata as such. Only processes which have invoked `forever.startServer()` will be available from `forever.list()`
217
218The `format` parameter is a boolean value indicating whether the returned values should be formatted according to the configured columns which can set with `forever columns` or programmatically `forever.config.set('columns')`.
219
220#### forever.tail (target, options, callback)
221Responds with the logs from the target script(s) from `tail`. There are two options:
222
223* `length` (numeric): is is used as the `-n` parameter to `tail`.
224* `stream` (boolean): is is used as the `-f` parameter to `tail`.
225
226#### forever.cleanUp ()
227Cleans up any extraneous forever *.pid files that are on the target system. This method returns an EventEmitter that raises the 'cleanUp' event when complete.
228
229#### forever.cleanLogsSync (processes)
230Removes all log files from the root forever directory that do not belong to current running forever processes. Processes are the value returned from `Monitor.data` in `forever-monitor`.
231
232#### forever.startServer (monitor0, monitor1, ..., monitorN)
233Starts the `forever` HTTP server for communication with the forever CLI. **NOTE:** This will change your `process.title`. This method takes multiple `forever.Monitor` instances which are defined in the `forever-monitor` dependency.
234
235### Logging and output file locations
236
237By default `forever` places all of the files it needs into `/$HOME/.forever`. If you would like to change that location just set the `FOREVER_ROOT` environment variable when you are running forever:
238
239```
240FOREVER_ROOT=/etc/forever forever start index.js
241```
242
243Make sure that the user running the process has the appropriate privileges to read & write to this directory.
244
245## Run Tests
246
247``` bash
248 $ npm test
249```
250
251#### License: MIT
252#### Author: [Charlie Robbins](https://github.com/indexzero)
253#### Maintainer: [Igor Savin](https://github.com/kibertoad)
254#### Contributors: [Fedor Indutny](https://github.com/indutny), [James Halliday](http://substack.net/), [Charlie McConnell](https://github.com/avianflu), [Maciej Malecki](https://github.com/mmalecki), [John Lancaster](http://jlank.com)
255
256[0]: https://github.com/foreverjs/forever-monitor
257[1]: https://github.com/foreverjs/forever-monitor/tree/master/examples
258[2]: https://github.com/foreverjs/forever/blob/master/lib/forever/cli.js