forever使用小结

 npm install forever -g   #安装


-g表示全局安装

forever命令语法及使用 https://github.com/nodejitsu/forever

There are two ways to use forever: through the command line or by using forever in your code.

If you are using forever programatically you should install forever-monitor.

 $ cd /path/to/your/project
  $ [sudo] npm install forever-monitor

(1) command line 用法:run scripts continuously (whether it is written in node.js or not).

forever start app.js
$ forever --help
  usage: forever [action] [options] SCRIPT [script-options]

  Monitors the script specified in the current process or as a daemon

  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            DEPRECATED. Process uid, useful as a namespace for processes (must wrap in a string)
                     e.g. forever start --uid "production" app.js
                         forever stop production
    --id             DEPRECATED. Process id, similar to uid, useful as a namespace for processes (must wrap in a string)
                     e.g. forever start --id "test" app.js
                         forever stop test
    --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)
    -t, --killTree   Kills the entire child process tree on `stop`
    --killSignal     Support exit signal customization (default is SIGKILL),
                     used for restarting script gracefully e.g. --killSignal=SIGTERM
    -h, --help       You're staring at it

  [Long Running Process]
    The forever process will continue to run outputting log messages to the console.
    ex. forever -o out.log -e err.log my-script.js

  [Daemon]
    The forever process will run as a daemon which will make the target process start
    in the background. This is extremely useful for remote starting simple node.js scripts
    without using nohup. It is recommended to run start with -o -l, & -e.
    ex. forever start -l forever.log -o out.log -e err.log my-daemon.js
        forever stop my-daemon.js
forever start -l forever.log -o out.log -e err.log my-daemon.js
        forever stop my-daemon.js

还有一种方式是把config写json里,

.
├── forever
│   └── development.json
└── index.js

// forever/development.json
{
	// Comments are supported
    "uid": "app",
    "append": true,
    "watch": true,
    "script": "index.js",
    "sourceDir": "/home/myuser/app"
}
$ forever start ./forever/development.json

或者

 forever start /home/myuser/app/forever/development.json

JSON configuration files can also be used to define the startup options for multiple applications, as shown below.

[
  {
    // App1
    "uid": "app1",
    "append": true,
    "watch": true,
    "script": "index.js",
    "sourceDir": "/home/myuser/app1"
  },
  {
    // App2
    "uid": "app2",
    "append": true,
    "watch": true,
    "script": "index.js",
    "sourceDir": "/home/myuser/app2",
    "args": ["--port", "8081"]
  }
]

(2)用在code中,这个我暂时没用。先到此为止



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

几个常用的命令

forever list   列出forever跑的程序
forever stop 0  stop第零号的进程


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个函数是用来在 MQTT 客户端的主程序中进行无限循环的。在循环中,它会处理从 MQTT 服务器接收到的消息,并维护与服务器的连接。 在执行此函数时,不需要使用 `time.sleep()` 进行延时。该函数会一直运行,直到遇到错误或者调用 `mqttc.disconnect()` 方法主动断开连接。 如果你希望在循环中暂停一段时间,你可以使用 `time.sleep()` 函数。但是请注意,如果使用过多的延时,会导致 MQTT 客户端的响应速度变慢,并且也会增加 CPU 的使用率。 ### 回答2: 在使用`mqttc.loop_forever()`函数时,通常不需要再使用`time.sleep()`进行延时。 `mqttc.loop_forever()`是一个阻塞函数,它会一直运行,直到连接断开或调用了`mqttc.disconnect()`。在循环中,它会处理接收到的消息,并调用相关的回调函数进行处理。此外,它还会处理心跳保持和重连等功能。 由于`mqttc.loop_forever()`是个持续运行的函数,它会自动处理消息的接收和处理,因此不需要额外的延时操作,也不需要使用 `time.sleep()` 函数。 在调用了`mqttc.loop_forever()`之后,程序会一直停留在这个函数内部,直到满足退出条件。因此,如果需要在`mqttc.loop_forever()`之后执行其他的任务,可以考虑将`mqttc.loop_forever()`放在一个单独的线程中运行,这样可以避免阻塞主线程,使得其他的任务能够继续执行。 总结来说,如果使用了`mqttc.loop_forever()`函数,一般不需要再用`time.sleep()`进行延时操作,但如果需要在`mqttc.loop_forever()`之后执行其他的任务,可以将其放在一个单独的线程中运行。 ### 回答3: 在使用mqttc.loop_forever()方法时,通常不需要再使用time.sleep()进行延时。因为mqttc.loop_forever()会一直运行,监听接收到的消息,并根据具体的逻辑进行处理。当没有新的消息到达时,mqttc.loop_forever()会以一种非阻塞的方式等待消息到达,不会占用过多的系统资源。 如果在mqttc.loop_forever()中使用time.sleep()进行延时,可能会导致消息处理的延迟,影响系统的实时性。因此,在正常情况下,不需要在mqttc.loop_forever()中使用time.sleep()进行延时。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值