[英]Running forever for NodeJs from rc.local
I want NodeJs server to start at boot and be sure it stays up if it crashes with forever. 我希望NodeJs服务器在启动时启动,并确保它永远崩溃时仍能启动。
I've read a lot of posts on how to do it and the possible issues associated with it. 我已经阅读了很多有关如何执行此操作以及与之相关的可能问题的文章。 The problem is that I have a limited memory available on given machine so I'm forced to "contain" NodeJs.
问题是给定机器上的可用内存有限,因此我被迫“包含” NodeJ。
Reading the forever documentation I found out that it can be done using the command: 阅读永久文档,我发现可以使用以下命令完成此操作:
forever start -c "node --max_old_space_size=512" myapp.js
In this way forever calls the node server passing the memory argument which, in this case, limits the heap to 512 MB. 通过这种方式,永远调用传递内存参数的节点服务器,在这种情况下,内存参数将堆限制为512 MB。 So I wrote my sh script for rc.local like this:
因此,我为rc.local编写了sh脚本,如下所示:
#!/bin/sh
cd /forever_bin_dir
./forever start -c "node --max_old_space_size=512" /myapp_dir/myapp.js
Since rc.local works with limited environmental variables, "node" can't be find and the script fails. 由于rc.local在有限的环境变量下工作,因此找不到“节点”,并且脚本失败。
I even tried with Cron adding to its file the line: 我什至尝试使用Cron在其文件中添加以下行:
@reboot /forever_bin_dir/forever start -c "node --max_old_space_size=512" /myapp_dir/myapp.js
But the same issue persist. 但是,同样的问题仍然存在。
My question is: how can i run forever at boot time passing at the same time the argument about memory ? 我的问题是:在传递有关内存的参数的同时,如何在引导时永远运行?
我发现此初始化脚本用于永久启动Node脚本的相同目的。
The best way I found It's just setting the PATH inside a script. 我发现的最好方法只是在脚本中设置PATH。
I created a sh script in the home folder called launchforever.sh I got the environmental variables using the #printenv command. 我在主文件夹中创建了一个称为launchforever.sh的sh脚本,使用#printenv命令获取了环境变量。
The launchforever script is something like this: launchforever脚本如下所示:
#!/bin/sh
export PATH=<copied from printenv>:$PATH
forever start -c "node --max_old_space_size=30" > /dev/null
After saving I gave it the permissions: 保存后,我给了它权限:
#chmod 700 /home/<user>/launchforever.sh
Then I added it to Cron: 然后我将其添加到Cron中:
#crontab -u <user> -e
And inside its file I added: 在文件中我添加了:
@reboot /home/<user>/launchforever.sh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.