在Ubuntu下安装nodejs4.x,并实现开机自动启动forever进程守护nodejs应用后台运行

来源:互联网 发布:手机录音放大软件 编辑:程序博客网 时间:2024/05/16 12:36
一、在Ubuntu下安装nodejs4.x

官方文档的方法最简单,不会出错

Node.js v4.x:    NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 4.x on older distros.# Using Ubuntu<strong>curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -sudo apt-get install -y nodejs</strong># Using Debian, as rootcurl -sL https://deb.nodesource.com/setup_4.x | bash -apt-get install -y nodejs

使用nodejs -v查看是否安装成功

来源官方文档

http://nodejs.cn/download/package-manager/#debian-and-ubuntu-based-linux-distributions

https://github.com/nodesource/distributions

二,开机自动启动forever进程守护nodejs应用后台运行

https://github.com/foreverjs/forever

超简单的forever,用到的主要命令

forever start app.js


查看所有正在运行的node进程

forever list


先写一个脚本文件,文件名是<filename>,文件里面要写全路径哦~

#!/bin/shforever start <the path is "/home....">/app.js
先看看脚本写对了没有

./<filename>

把这个脚本文件复制到 /etc/init.d目录下去

sudo cp -i <filename> ./etc/init.d
设置脚本文件的权限

sudo chmod +x /etc/init.d/test
将脚本放到启动脚本中去
cd /etc/init.dsudo update-rc.d <filename> defaults <number>
<number>是这个脚本的启动的顺序号,去这个etc文件夹下面的这几个文件夹中看看,选个合适的数字


输出参考如下 http://rongjih.blog.163.com/blog/static/33574461201111504843245/

不想启动这个脚本啦?
卸载启动脚本的方法:
cd /etc/init.dsudo update-rc.d -f <filename> remove
命令输出的信息和上图有点像,不贴了


三、关于forever的一些注意事项

开机自动启动的forever启动的node进程用forever list命令并不能查到,想要查应该用linux系统的命令

ps -ax

然后会看到所有的进程,由于开机启动的原因,forever monitor的进程和node的进程比较靠前

如果此时修改node app的代码,会node的进程依旧完美运行。

想要让改动生效的话,请查看node的app.js进程的pid

sudo kill <pid>
然后forever 会自动重启node进程,完美~

用list命令可以发现有新的node进程,pid是新的
0 0
原创粉丝点击