Node - Trying to restart using Forever won't work in bash script on cronjob

空扰寡人 提交于 2019-12-22 17:39:23

问题


I have a simple bash script written that auto-deploys code to my Node.js server whenever a commit is made, then restarts the server using forever. This script is run periodically on a cronjob. Oh yeah, the site is still in the dev phase before being released, hence the constant server restarting. :)

The script does some other things, but the real interesting part is the following two lines of code:

forever stopall
forever start app.js

When I run the script manually from the command-line myself, the following output occurs from those two commands:

Stopping all node jobs
info:    Forever stopped processes:
data:        uid  command             script forever pid   logfile                          uptime
data:    [0] ev5a /usr/local/bin/node app.js 17482   17484 /home/ec2-user/.forever/ev5a.log 0:0:13:17.967
Starting up node server again
info:    Forever processing file: app.js

However, when the job is run automatically via cron, this is what gets outputted:

Stopping all node jobs
^[[32minfo^[[39m:    No forever processes running
Starting up node server again
^[[32minfo^[[39m:    Forever processing file: ^[[90mapp.js^[[39m

This is really strange to me. It appears that when the script is run via cron, Forever seems to think there isn't even any Node process running when it tries to stopall. I know for a fact that the Node process is running, so not sure why this is showing up. It seems that there shouldn't be a difference between the two run methods. Both scripts are running as the same user. Does anyone know why forever is behaving strangely like this?


回答1:


forever is generally limited to processes owned by the current user. If the user firing the cron job is not the same user that originally started the process, then forever stopall will not find any PIDs to kill.

For example, I execute all of my node apps using a user called "nodejs". If I'm using the system interactively, I can use sudo -H -u nodejs forever list. And on root crons or /etc/rc.local, I will use su - nodejs -c "forever start /path/to/app.js"




回答2:


I haven't tested it, but you might want to look into some of forever's options such as the --exitcrash flag.

Forever has issues when dealing with other programs that automatically restart your server. While not specific to Chron, this link might help.



来源:https://stackoverflow.com/questions/15278993/node-trying-to-restart-using-forever-wont-work-in-bash-script-on-cronjob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!