系统重启时自动永远启动(节点) [英] Automatically start forever (node) on system restart

查看:24
本文介绍了系统重启时自动永远启动(节点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用节点的永久模块来保持我的节点服务器运行.但是,当系统重新启动时,Forever 会终止.有什么办法可以在系统重启时自动启动节点服务器(永远)?

I am using node's forever module to keep my node server running. Forever however terminates when there is a system restart. Is there any way I can automatically start the node server (with forever) when the system restarts?

推荐答案

我建议使用 crontab.它易于使用.

I would suggest using crontab. It's easy to use.

  1. 要开始编辑,请运行以下命令,将testuser"替换为节点进程所需的运行时用户.如果您选择自己以外的其他用户,则必须使用 sudo 运行.

  1. To start editing run the following replacing the "testuser" with your desired runtime user for the node process. If you choose a different user other than yourself, you will have to run this with sudo.

$ crontab -u testuser -e

  • 如果您以前从未这样做过,它会询问您希望使用哪个编辑器进行编辑.我喜欢 vim,但会推荐 nano 以方便使用.

  • If you have never done this before, it will ask you which editor you wish to edit with. I like vim, but will recommend nano for ease of use.

    在编辑器中添加以下行:

    Once in the editor add the following line:

    @reboot /usr/local/bin/forever start /your/path/to/your/app.js
    

  • 保存文件.您应该会收到一些关于 cron 已安装的反馈.

  • Save the file. You should get some feedback that the cron has been installed.

    要进一步确认 cron 的安装,请执行以下命令(再次将testuser"替换为您的目标用户名)以列出当前安装的 cron:

    For further confirmation of the installation of the cron, execute the following (again replacing "testuser" with your target username) to list the currently installed crons:

    $ crontab -u testuser -l 
    

  • 请注意,在我看来,在 cron 中执行二进制文件时,您应该始终使用完整路径.此外,如果您的永久脚本的路径不正确,请运行 which forever 以获取完整路径.

    Note that in my opinion, you should always use full paths when executing binaries in cron. Also, if the path to your forever script is not correct, run which forever to get the full path.

    鉴于 forever 调用 node,您可能还需要提供 node 的完整路径:

    Given that forever calls node, you may also want to provide the full path to node:

    @reboot /usr/local/bin/forever start -c /usr/local/bin/node /your/path/to/your/app.js
    

    进一步阅读

    • crontab 手册页
    • Ubuntu Cron 方法
    • 这篇关于系统重启时自动永远启动(节点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆