crontab 在生产模式下永远启动节点应用程序

Crontab start node application with forever in production mode

我有一个 node.js 应用程序,我会像这样永远使用它:

NODE_ENV=production forever start index.js

我还研究了如何设置 crontab 以在服务器重新启动时自动启动此应用程序的 forever 命令:

@reboot /usr/local/bin/forever start /path/to/my/app/index.js

这里唯一的问题是节点环境。如何在crontab命令中添加生产环境?

如果您需要在 crontab 中执行带有变量等的特殊命令,编写一个简单的 shell 脚本并从 crontab 调用该脚本会更容易:

#!/bin/bash
export NODE_ENV=production
/usr/local/bin/forever start /path/to/my/app/index.js

使其可执行:chmod 755 /usr/local/bin/start_my_app.sh

然后在你的 crontab 中:

@reboot /usr/local/bin/start_my_app.sh

如果只想设置一个环境变量,可以在 forever 命令之前使用 export 命令。

@reboot export NODE_ENV=production; /usr/local/bin/forever start /path/to/my/app/index.js

对于一个以上的变量,Sukima 的方法更好。