"Nodejs" uses forever management Nodejs__js

Source: Internet
Author: User
Tags app service time interval node server

This article mainly introduces the use of Forever Management Nodejs Application tutorial, this article introduced the Forever installation, commonly used commands, etc., the most useful is the file change monitoring and automatic restart, which can increase the efficiency of the application of open Nodejs, the need for friends can refer to the

What is Foreve

Forever can be seen as a Nodejs daemon that can start, stop, and restart our app apps.
The official note is that:
The copy code code is as follows: A simple CLI tool for ensuring that a given script runs continuously (i.e. forever).
A simple command-line tool for continuing (or forever) running a given script
GitHub Address: Https://github.com/nodejitsu/forever

Forever Use

The purpose of forever is to help us better manage our Node app service, essentially creating a node app's subprocess under the forever process.
For example, if you have a express based or some other application, it will be very convenient for you to update and operate your service, and ensure that your service continues to operate.
The better thing is that every time you change a file, it automatically restarts the service without having to restart it manually.

Install Forever
Copy code code as follows:
Remember to add-g,forever requirements to install to the global environment
sudo npm install forever-g

Forever Use Instructions

Start related

Copy code code as follows:
1. Easy to start
Forever Start App.js

2. Specify forever information output file, of course, by default it will be placed in ~/.forever/forever.log
Forever Start-l Forever.log App.js

3. Specifies the log information and error log output files in App.js.
-O is the Console.log output information, E is the Console.error output information
Forever Start-o out.log-e Err.log app.js

4. Append log, forever default is unable to overwrite the last boot log,
So if the second boot does not add-a, it will not let the running
Forever Start-l forever.log-a App.js

5. Listen for all file changes under the current folder
Forever Start-w App.js

file changes are monitored and automatically restarted

Copy code code as follows:
1. Listens for all file changes under the current folder (this is not recommended)
Forever Start-w App.js
Show all running Services
Copy code code as follows: Forever list
Stop operation
Copy code code as follows://1. Stop all the running node apps
Forever StopAll

2. Stop one of the node apps
Forever Stop App.js
Of course I can.
The Forever list finds the corresponding ID, and then:
Forever Stop [ID]

Restart Operation

The restart operation is consistent with the stop operation.
Copy code code as follows://1. Start all
Forever Restartall

Development and online recommended configuration
Copy code code as follows:
Under development environment
Node_env=development Forever start-l forever.log-e err.log-a app.js
In the online environment
Node_env=production Forever start-l ~/.forever/forever.log-e ~/.forever/err.log-w-a app.js
Add node_env to allow App.js to identify what the current environment is. If you don't add it, you may not know it.

some attention.

It is possible that you need to use the crontab (timed task) under Unix
This time you need to pay attention to configuring environment variables.
Copy code code as follows: Shell=/bin/sh
Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Third, the Forever command line in Chinese interpretation

Child Command actions:
Copy code code as follows:
Start: Starting the daemon
Stop: Stopping the daemon
StopAll: Stop All Forever processes
Restart: Reboot daemon
Restartall: Reboot all Foever processes
List: Listing shows forever process
Config: Lists all user Configuration items
Set <key> <val>: Setting User Configuration Items
Clear <key>: Clearly User Configuration items
Logs: Lists the logs for all forever processes
Logs <script|index&gt: Display the latest log
Columns add <col>: Customizing metrics to Forever List
Columns rm <col&gt: Delete Forever list metrics
Columns Set<cols&gt: Set all metrics to Forever list
Cleanlogs: Delete all Forever history logs
Configuration Parameters Options:
Copy code code as follows:
-M MAX: number of times the specified script was run
-L LOGFILE: output log to LOGFILE
-o outfile: output console information to outfile
-E errfile: Output console Error in Errfile
-P PATH: root directory
-C command: Execute commands, default is node
-a,–append: Merge Logs
-f,–fifo: Streaming Log output
-n,–number: Number of log print lines
–pidfile:pid file
–sourcedir: Source code Directory
–minuptime: Minimum spinn update time (MS)
–spinsleeptime: Two times spin time interval
–colors: Console Output coloring
–plain:–no-colors alias, console output colorless
-d,–debug:debug mode
-v,–verbose: Print verbose output
-s,–silent: Do not print logs and error messages
-w,–watch: Monitoring file changes
–watchdirectory: Monitoring the top-level directory
–watchignore: Ignoring monitoring through pattern matching
-h,–help: Command line help information


The above command looks at the forever Help file first, then runs App.js, and then stops app.js. We want forever to run automatically, and first create a file node in the/ETC/INIT.D directory, which reads as follows:
Copy code code as follows:
#!/bin/bash
#
# node Start up node Server daemon
#
# chkconfig:345 85 15
# Description:forever for Node.js
#
Path=/home/node/0.8.9/bin
Deamon=/home/ftp/1520/weizt-20120918-tkx/weizt.com/app.js
Log=/home/hosts_log
Pid=/tmp/forever.pid
Case "$" in
Start
Forever start-l $LOG/forever.log-o $LOG/forever_out.log-e $LOG/forever_err.log--pidfile $PID-a $DEAMON

Stop
Forever Stop--pidfile $PID $DEAMON

StopAll)
Forever StopAll--pidfile $PID

Restartall)
Forever Restartall--pidfile $PID

Reload|restart)
Forever restart-l $LOG/forever.log-o $LOG/forever_out.log-e $LOG/forever_err.log--pidfile $PID-a $DEAMON

List
Forever List

*)
echo "Usage:/etc.init.d/node {start|stop|restart|reload|stopall|restartall|list}"
Exit 1

Esac
Exit 0
The above code is my local virtual machine configuration, according to the actual situation to modify the relevant parameters, mainly Deamon path parameters, give the file executable permissions, and run Chkconfig add automatic run:
Copy code code as follows:
chmod 755/etc/init.d/node
Chkconfig/etc/init.d/node on
Reboot restart the system, through the browser to enter the site can be found, the NODEJS has been able to automatically run, the rest of the work is to study Nodejs, Express and Angularjs, to do a really belong to their own applications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.