Restart ruby program
I am writing an IRC bot and my intention is to have "!reboot" reboot the bot. I have already got it to part from a channel and break the running loop, but I cannot get it to re-r开发者_Go百科un the file.
It needs to start a new process and load up a new version of the file, so that any new commands etc. and configuration changes can be loaded.
Just run Kernel.exec
function that will replace running process with the new one.
The advantage of exec
function is that there's no time span when two different bots are run simultaneously. It was one process and after exec
call it's instantly replaced with the new one.
You could rerun the bot by using a system command system("ruby /path/to/my/bot.rb")
when a restart is required.
It is not something I would do without a very good reason. It seems a nicer solution to build the bot in such a way that it is able to just reload new commands and configuration changes at any time, so a restart is not required at all.
A couple of possibilities, one simple, one probably less so.
First, could you put the irb execution into a looping command/shell script file? Then your reboot becomes a simple exit
and the script jumps back and runs your Ruby file again. If a different file is to be used, the name (or path) could be put into an environment variable before exit.
As an alternative, could you call eval
with your rewritten script as argument? It's one of the techniques used in Giles Bowkett's Archaeopteryx - change the code and save it, then (in this instance) a timed loop picks up the new code and runs it using eval
.
精彩评论