[英]How to execute and manage ruby script from ruby?
I have a script named program.rb and would like to write a script named main.rb that would do the following: 我有一个名为program.rb的脚本,并希望编写一个名为main.rb的脚本来执行以下操作:
system("ruby", "program.rb")
constantly check if program.rb is running until it is done
if program.rb has reached completion
exit main.rb
end
otherwise keep doing this until program.rb reaches completion{
if program.rb is not running and stopped before completing
restart program.rb from where it left off
end}
I've looked into Pidify but could not find a way to apply it to fit this exactly the right way... Any help in how to approach this script would be greatly appreciated! 我已经研究过Pidify,但无法找到一种方法来应用它以完全正确的方式...任何有关如何处理此脚本的帮助将不胜感激!
Update: I could figure out how to resume running the script from where it left off in program.rb if there's no way to do it in main.rb 更新:如果在main.rb中无法执行此操作,我可以弄清楚如何在program.rb中从中断处继续运行脚本
It's impossible to "restart script from where it left off" without full cooperation from the program.rb
. 如果没有程序的完全合作,就不可能“从中断的地方重新启动脚本”
program.rb
。 That is, it should be able to advertise its progress (by writing current state to a file, maybe?) and be able to start correctly from a step specified in ARGV. 也就是说,它应该能够通告其进度(通过将当前状态写入文件,可能?)并且能够从ARGV中指定的步骤正确启动。 There's no external ruby magic that can replace this functionality.
没有外部红宝石魔法可以取代这个功能。
Also, if a program terminated abnormally, it means one of two things: 此外,如果程序异常终止,则表示以下两种情况之一:
In either case, there's no need for restarting, IMHO. 在任何一种情况下,都不需要重启,恕我直言。
Well, here is one way. 嗯,这是一种方式。
Modify program.rb
to take an optional flag argument --restart
or something. 修改
program.rb
以获取可选的标志参数--restart
或其他东西。
When program.rb
starts up without this argument it will initialize a file to record its current state. 当
program.rb
在没有此参数的情况下启动时,它将初始化文件以记录其当前状态。 Periodically, it will write whatever it needs into this file to record some kind of checkpoint. 它会定期将所需内容写入此文件以记录某种检查点。
When program.rb
starts up with the restart flag, it will read its checkpoint file and start processing at that point. 当
program.rb
与 启动标记启动时,它会读取它的检查点文件,并在该点开始加工。 For this to work, it must either checkpoint all state changes or arrange for all processing between checkpoints to be idempotent so it can be repeated without ill effect. 要使其工作,它必须检查所有状态更改或安排检查点之间的所有处理是幂等的,以便可以重复它而不会产生不良影响。
There are lots of ways to monitor the health of program.rb
. 有很多方法可以监控
program.rb
的运行状况。 The best way is with some sort of ping, perhaps something like GET /health_check
or a dummy message via a socket or pipe. 最好的方法是使用某种类型的ping,可能是类似
GET /health_check
或通过套接字或管道的虚拟消息。 You could just have a locked file to detect if the lock is still held, or you could record the PID on startup and check that it still exists. 您可以只有一个锁定的文件来检测锁是否仍然存在,或者您可以在启动时记录PID并检查它是否仍然存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.