ブート時に起動して www-data ユーザーの下で実行する必要がある node.js スクリプトがあります。開発中、私は常にスクリプトを次のように開始しました。
su www-data -c 'node /var/www/php-jobs/manager.js
私は何が起こったのかを正確に見ました.manager.jsは今ではうまく機能します. SOを検索すると、これを自分のに配置する必要があることがわかりました/etc/rc.local
。また、出力をログ ファイルにポイントし、2>&1
「stderr を stdout にリダイレクトする」に を追加することを学びました。これはデーモンである必要があるため、最後の文字は&
.
最後に、私の/etc/rc.local
見た目は次のようになります。
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
su www-data -c 'node /var/www/php-jobs/manager.js >> /var/log/php-jobs.log 2>&1 &'
exit 0
これを自分で実行すると ( sudo /etc/rc.local
): はい、動作します! ただし、node
プロセスが実行されて/var/log/php-jobs.log
いない再起動を実行すると、存在しないため、manager.js が機能しません。何が起こっている?