Karthick Karthick - 1 month ago 20
Ruby Question

ruby restart script automatically once it ends execution

I have a ruby script which do some stuffs for long and its not predictable how long it will take to finish. But I want the script execution to be restarted automatically once it finishes the execution. Is there any possibilities to make it work in Ruby 1.8.7?

Answer

I have a ruby daemon running on my server and for some mysterious reasons it crashes some times. I wrapped it into a while true loop and print me some timestamps to stdout, like that:

#!/bin/bash
while true; do
    echo $(date +%Y%m%d%H%M%S) "Starting my custom ruby daemon..."
    ruby /home/user/my/custom/ruby/daemon.rb
    echo $(date +%Y%m%d%H%M%S) "My custom ruby daemon crashed. Respawning in 10 sec..."
    sleep 10
done

That works well. Putting a sleep into it is highly recommended, in case something goes wrong the loop wont blow up your shell.