Restart ruby ​​program

I am writing an IRC bot, and my intention is to restart the bot reboot. I already got it to part with the channel and break the running cycle, but I can not get it to re-run the file.

He needs to start a new process and upload a new version of the file so that he can download any new commands, etc. and configuration changes.

+4
source share
3 answers

Just run the Kernel.exec function, which will replace the current process with a new one.

The advantage of the exec function is that there is no time when two different bots are launched at the same time. It was one process, and after exec it was immediately replaced with a new one.

+5
source

You can restart the bot using the system command system("ruby /path/to/my/bot.rb") when a reboot is required.

This is not something I would do without a good reason. It seems that it is better to build the bot so that it can simply reload new commands and configuration changes at any time, so a reboot is not required at all.

+2
source

Several possibilities, one simple, perhaps less.

First, could you put irb execution in a loop / shell script file? Then your reboot will become a simple exit , and the script will bounce back and run your Ruby file again. If another file is used, the name (or path) can be placed in the environment variable before exiting.

Alternatively, could you call eval your rewritten script argument? This is one of the methods used by Giles Bowkett Archaeopteryx - change the code and save it, then (in this case) selects a synchronized loop to update the new code and start it using eval .

0
source

All Articles