[英]Crontab start node application with forever in production mode
I have a node.js application that I start using forever like so: 我有一个node.js应用程序,我开始永远使用,如下所示:
NODE_ENV=production forever start index.js
I've also worked out how to setup crontab to automatically start the forever command for this application on server reboot: 我还研究了如何设置crontab以在服务器重启时自动启动此应用程序的forever命令:
@reboot /usr/local/bin/forever start /path/to/my/app/index.js
The only problem here is the node environment. 这里唯一的问题是节点环境。 How do I add the production environment to the crontab command?
如何将生产环境添加到crontab命令?
If you need to execute a special command with variables etc. in a crontab it's easier to write a simple shell script and call that script from the crontab: 如果你需要在crontab中执行带变量等的特殊命令,那么编写一个简单的shell脚本并从crontab调用该脚本会更容易:
#!/bin/bash
export NODE_ENV=production
/usr/local/bin/forever start /path/to/my/app/index.js
Make it executable: chmod 755 /usr/local/bin/start_my_app.sh
使其可执行:
chmod 755 /usr/local/bin/start_my_app.sh
Then in your crontab: 然后在你的crontab中:
@reboot /usr/local/bin/start_my_app.sh
If you want to set just one environment variable, you could use the export command right before the forever command. 如果只想设置一个环境变量,可以在forever命令之前使用export命令。
@reboot export NODE_ENV=production; /usr/local/bin/forever start /path/to/my/app/index.js
For more than one variable Sukima's method is better. 对于多个变量,Sukima的方法更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.