Automatically start forever (node) on system restart Ask Question

Automatically start forever (node) on system restart Ask Question

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?

ベストアンサー1

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

How to

  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
    
  2. 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.

  3. Once in the editor add the following line:

    @reboot /usr/local/bin/forever start /your/path/to/your/app.js
    
  4. Save the file. You should get some feedback that the cron has been installed.

  5. cron のインストールをさらに確認するには、次のコマンドを実行し (ここでも「testuser」を対象のユーザー名に置き換えます)、現在インストールされている cron を一覧表示します。

    $ crontab -u testuser -l 
    

私の意見では、cron でバイナリを実行するときは常にフルパスを使用する必要があることに注意してください。また、forever スクリプトへのパスが正しくない場合は、を実行してwhich foreverフルパスを取得します。

foreverがを呼び出す場合node、 へのフルパスも指定する必要がありますnode

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

参考文献

おすすめ記事