简体   繁体   中英

Crontab start node application with forever in production mode

I have a node.js application that I start using forever like so:

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:

@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?

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:

#!/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

Then in your 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.

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM