LoginSignup
2
3

More than 5 years have passed since last update.

macでnodejsのデーモン化(foreverを使う)

Posted at

macでplistを作成してnodejsのデーモン化を行なった。

まずはnodejsとnpmは入ってる前提で話進めます。
入ってないひとはこちら
linuxならrc.localに入力すればいいけどmacはそれができない。
plistを使ってあげましょう。

まずはforeverをインストール

npm install -g forever 

plistの作成

username は適当に自分のを入力

~/Library/LaunchAgents/com.example.test.test.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>UserName</key>
        <string>username</string>
        <key>KeepAlive</key>
        <true/>
        <key>Label</key>
        <string>com.example.test.test</string>
        <key>ProgramArguments</key>
        <array>
                <string>sh</string>
                <string>/Users/username/start_forever.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    <key>StandardOutPath</key>
    <string>/path/to/standard.log</string>
    <key>StandardErrorPath</key>
    <string>/path/to/error.log</string>
</dict>
</plist>

shスクリプト

これで保存nanoかvi、vimでファイルを作成

bash
nano /Users/$USER/start_forever.sh
/Users/$USER/start_forever.sh
/usr/local/bin/node /usr/local/bin/forever /path/to/app.js
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3