找的都不靠谱,试了一轮了,求指导。
how what why 作为守护进程肯定有用,至于你说的试了一轮,总归会出问题。贴出来可以帮你解决。问这样的问题没有任何意义; demo.js
setInterval(function () {
setTimeout(function () {
console.log('this question is not sense', name);
}, 5000);
console.log('this question is not sense');
}, 1000);
pm2 start demo.js
曾经也被pm2自启的问题困扰很久, 下面的方法AWS和DigitalOcean Ubuntu亲测可用 I ran into this issue, and found a solution that works for me. Let’s assume I’ve created a user named ‘node’ to run pm2. All of the commands below should be run by the node user, in the node user’s home directory (/home/node)
- Clear any pm2 dump files, and then start the appropriate services
pm2 delete all
pm2 dump
pm2 start app.js --watch
- Save the current config to start on reboot
pm2 save
sudo pm2 startup centos -u node
- Edit the PM2_HOME path in the pm2-init script to match the home directory of the user:
sudo vi /etc/init.d/pm2-init.sh
export PM2_HOME="/home/node/.pm2"
I found the issue was, the user was being set appropriately in the pm2-init.sh file, but the PM2_HOME directory was set to /root/.pm2 by default
==UPDATE==
- For centos, I was able to replace step 2 above with the following:
sudo su -c "env PATH=$PATH:/usr/local/bin PM2_HOME=/home/node/.pm2 pm2 startup centos -u node"
NAME=pm2 PM2=/usr/local/lib/node_modules/pm2/bin/pm2 USER=node
export PATH=/usr/local/bin:$PATH export PM2_HOME="/home/node/.pm2"
lockfile="/var/lock/subsys/pm2-init.sh"
super() { su - $USER -c “PATH=$PATH; PM2_HOME=$PM2_HOME $*” }
这里分享一下,给遇到问题的朋友。同时也分享下服务器安全的一些小配置。 1、在root权限下,新建一个node用户 useradd node
2、创建密码 passwd node 后提示输入密码。
3、加入sudoers visudo 在 ## Allow root to run any commands anywhere root ALL=(ALL) ALL 这里,加入 node ALL=(ALL) ALL 按esc后,输入:wq保存。
4、提一下用yum安装nodejs,不用源码编译。 安装nodejs 4.x LTS的 先 curl --silent --location https://rpm.nodesource.com/setup_4.x | bash - (参考这里 https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora)
然后 yum clean all,最好reboot一下 yum install nodejs,检查一下是不是4.2.X版本,再同样。 安装后 node -v 检查一下。
这种方法安装的node的路径会不同,直接就在/usr/bin下了,如果是源码编译的,会在/usr/local/bin下
5、如果是用4的方法安装的node,就可以跳过 以下适用与源码编译的nodejs,做link sudo ln -s /usr/local/bin/node /usr/bin/node sudo ln -s /usr/local/lib/node /usr/lib/node sudo ln -s /usr/local/bin/npm /usr/bin/npm sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf
6、安装pm2 pm2 delete all pm2 dump NODE_ENV=production; pm2 start app.js 等一会,稳定后, pm2 save sudo pm2 startup centos -u node (这条命令后,有一段提示,要按照提示输入下一条命令,请注意看啊) su -c “chmod +x /etc/init.d/pm2-init.sh; chkconfig --add pm2-init.sh" 然后编辑PM2_HOME sudo vi /etc/init.d/pm2-init.sh export PM2_HOME=”/home/node/.pm2"
我的/etc/rc.d/init.d/pm2-init.sh大致如下: NAME=pm2 PM2=/usr/lib/node_modules/pm2/bin/pm2 USER=node
export PATH=/usr/bin:$PATH export PM2_HOME="/home/node/.pm2"
lockfile="/var/lock/subsys/pm2-init.sh"
6、重点!! chattr +i /home/node/.pm2/dump.pm2 因为这货会在重启后被清空,然后就不能自启动了。
7、重启试试
参考: http://www.hkpug.net/2014/09/18/安裝-centos-7-後必做的七件事/ http://kongfangyu.com/2015/12/03/aliyun-nodejs/ https://github.com/Unitech/pm2/issues/1521