- Edited
nginx -s reload时,init_by_lua中的代码被执行了2次,是什么原因呢?
- Edited
Hello! 2013/12/21 小冶: > 看起来像是老进程结束前执行了一次,新进程起来后又执行了一次 > 我不知道为什么老进程在结束时为什么也要执行一次?这明显不是它的init阶段啊 > 如果这里的代码对于执行次数是敏感的(不能随便重复执行),那该怎么办噢?? > 首先,使用 nginx -s reload 是不推荐的,推荐使用 kill -HUP `cat logs/nginx.pid` 命令来让 nginx 重新加载。根据 nginx 官方的说明, -s reload 的引入主要是为了支持 Windows 系统,在 UNIX 风格的系统上可以基本工作,但容易引起一些问题。 其次,针对你的问题,init_by_lua 是在 Lua VM 创建后调用的,而每当 nginx 加载配置文件时,都会创建一个新的 Lua VM. 当你使用 nginx -s xxx 命令时(不论是 nginx -s reload 还是 nginx -s stop),nginx 需要首先读取它的配置文件,所以会多加载一次配置,也就是多创建一回 Lua VM(虽然第一个创建的 Lua VM 很快就会被丢弃)。 Best regards, -agentzh
- Edited
在 2013年12月22日星期日UTC+8上午11时14分13秒,小冶写道:
看起来像是老进程结束前执行了一次,新进程起来后又执行了一次我不知道为什么老进程在结束时为什么也要执行一次?这明显不是它的init阶段啊 如果这里的代码对于执行次数是敏感的(不能随便重复执行),那该怎么办噢??
- Edited
在 2013年12月22日星期日 UTC+8上午11:14:13,小冶写道:
看起来像是老进程结束前执行了一次,新进程起来后又执行了一次我不知道为什么老进程在结束时为什么也要执行一次?这明显不是它的init阶段啊 如果这里的代码对于执行次数是敏感的(不能随便重复执行),那该怎么办噢??
- Edited
restart:先stop 再 start,就像关闭浏览器再打开一样。
参考:http://www.code009.com/article/22037
在 2013年12月22日星期日 UTC+8下午1:56:23,agentzh写道:
Hello!
2013/12/21 小冶:
> 看起来像是老进程结束前执行了一次,新进程起来后又执行了一次
> 我不知道为什么老进程在结束时为什么也要执行一次?这明显不是它的init阶段啊
> 如果这里的代码对于执行次数是敏感的(不能随便重复执行),那该怎么办噢??
>
首先,使用 nginx -s reload 是不推荐的,推荐使用 kill -HUP `cat logs/nginx.pid` 命令来让
nginx 重新加载。根据 nginx 官方的说明, -s reload 的引入主要是为了支持 Windows 系统,在 UNIX
风格的系统上可以基本工作,但容易引起一些问题。
其次,针对你的问题,init_by_lua 是在 Lua VM 创建后调用的,而每当 nginx 加载配置文件时,都会创建一个新的 Lua
VM. 当你使用 nginx -s xxx 命令时(不论是 nginx -s reload 还是 nginx -s stop),nginx
需要首先读取它的配置文件,所以会多加载一次配置,也就是多创建一回 Lua VM(虽然第一个创建的 Lua VM 很快就会被丢弃)。
Best regards,
-agentzh
- Edited
在 2016年5月19日星期四 UTC+8上午10:13:11,Ahlfors Lee写道:
Hi, agentzh,请教下,关于Nginx Reload和Restart的区别,不知道大家平时怎么使用。stackoverflow上的两个Answser:看上去蛮有道理的。有人提到这两者区别:reload:不间断服务重启,就像一张网页上面的刷新按钮一样。
restart:先stop 再 start,就像关闭浏览器再打开一样。
参考:http://www.code009.com/article/22037 我在线上修改lua脚本后,是否直接用reload就可以了?和你提到的 -s reload的使用注意,有区别吗? 感谢!Ahlfors
在 2013年12月22日星期日 UTC+8下午1:56:23,agentzh写道:Hello!
2013/12/21 小冶:
> 看起来像是老进程结束前执行了一次,新进程起来后又执行了一次
> 我不知道为什么老进程在结束时为什么也要执行一次?这明显不是它的init阶段啊
> 如果这里的代码对于执行次数是敏感的(不能随便重复执行),那该怎么办噢??
>
首先,使用 nginx -s reload 是不推荐的,推荐使用 kill -HUP `cat logs/nginx.pid` 命令来让
nginx 重新加载。根据 nginx 官方的说明, -s reload 的引入主要是为了支持 Windows 系统,在 UNIX
风格的系统上可以基本工作,但容易引起一些问题。
其次,针对你的问题,init_by_lua 是在 Lua VM 创建后调用的,而每当 nginx 加载配置文件时,都会创建一个新的 Lua
VM. 当你使用 nginx -s xxx 命令时(不论是 nginx -s reload 还是 nginx -s stop),nginx
需要首先读取它的配置文件,所以会多加载一次配置,也就是多创建一回 Lua VM(虽然第一个创建的 Lua VM 很快就会被丢弃)。
Best regards,
-agentzh
- Edited
Hello! 2016-05-18 19:13 GMT-07:00 Ahlfors Lee: > 请教下,关于Nginx Reload和Restart的区别,不知道大家平时怎么使用。 > stackoverflow上的两个Answser: > http://serverfault.com/questions/378581/nginx-config-reload-without-downtime > http://stackoverflow.com/questions/13525465/when-to-restart-and-not-reload-nginx > 看上去蛮有道理的。 > 有人提到这两者区别: > reload:不间断服务重启,就像一张网页上面的刷新按钮一样。 > restart:先stop 再 start,就像关闭浏览器再打开一样。 其实还有一种热替换升级方式: http://nginx.org/en/docs/control.html#upgrade 这是没有 stop 和 start 之间的 down time 的(完全没有,而不是很小)。 这种热升级的方法适用于更新二进制组件,比如升级 OpenResty 或 Nginx 自身。 HUP reload 方法不会更新二进制程序,只是更新配置和 Lua 代码,同时也不会丢弃共享内存中已有的数据。 Regards, -agentzh
读取文件才能找到对应的 pid 存储在哪个文件,这样才能给指定进程发送信号
It looks like it was executed once before the old process ended, and again after the new process started.
I don't know why the old process has to be executed once when it ends? This is obviously not its init phase.
What if the code here is sensitive to subway menu the number of executions (it cannot be repeated at will), then what should I do? ?
If the old process is executing again right before it ends, it could be due to cleanup or finalization code that’s triggering one last execution. To prevent unintended repeated executions, especially if the code is sensitive to this, here are some steps you can try:
Check the Termination Sequence: Review the shutdown/exit phase of the old process to see if there’s code that’s unintentionally re-triggering execution.
Add Execution Guards: Implement a flag (e.g., executed = false) that only allows the process to execute if it hasn’t already run. Once executed, set the flag to prevent any further runs.
Log Execution Points: Add logging around execution points to help pinpoint where and why the process is running again. This can clarify if it’s due to a particular phase or unexpected re-trigger.
Modify Code Sensitivity: If possible, modify the sensitive code to handle a final execution gracefully, especially if cleanup is necessary.
This approach should help control the process execution and avoid unintended repeats.