Take the 2-minute tour ×

I'm using linux. I want a process (an irc bot) to run every time I start the computer. But I've got a problem: The network is bad and it disconnects often, so I need to manually restart the bot a few times a day. How do I automate that?

Additional information: The bot creates a pid file, called bot.pid The bot reconnects itself, but only a few times. The network is too bad, so the bot kills itself sometimes because it gets no response.

What I do currently (aka my approach ;) ) I have a cron job executing startbot.rb every 5 minutes. (The script itself is in the same directory as the bot)

The script:

require 'fileutils'

if File.exists?(File.expand_path('tmp/bot.pid'))
  @pid = File.read(File.expand_path('tmp/bot.pid')).chomp!.to_i
    raise "ouch" if Process.kill(0, @pid) != 1
    puts "Removing abandoned pid file"
    puts "Starting the bot!"
    puts "Bot up and running!"
  puts "Starting the bot!"

What this does: It checks if the pid file exists, if that's true it checks if kill -s 0 BOT_PID == 1 (if the bot's running) and starts the bot if one of the two checks fail/are not true.

My approach seems to be quite dirty so how do I do it better?

share|improve this question
Monit can be a very handy utility for things like this. Configuration is not onerous, either. Here: mmonit.com/monit/documentation/monit.html and here: mmonit.com/wiki/Monit/ConfigurationExamples –  thisfeller Nov 29 '12 at 16:22
Also, take a look at swatch and dwatch. They are watch daemons that can do a variety of things when a process is halted. They can even reboot the machine in some circumstances. –  Ian Atkin Nov 29 '12 at 16:58
It seems that you may want to use the Process class to help you manage your child processes. –  vgoff Nov 29 '12 at 23:07
ok thx, I did something similar: I wrote a quick loop script which runs my script as soon as it quits, what it does as soon as the bot kills itself ;) –  le_me Nov 30 '12 at 7:48
add comment

1 Answer

up vote 1 down vote accepted

I cross-posted my question to stackoverflow.com, where I got the answer ;)


Restarting a application is a bad workaround, not a solution.

i recommend to review the documentation of your bot, look for a option to configure after how many bad retries it exits or how to disable this functionality completely. if the bot is open source, you can also review its source code and modify the retry code. Try to find a clean solution.

if you really want to restart it, i would create a shellscript that runs the bot in a loop. make sure bot.rb does not fork into the background:

for (( ; ; ))

you can run that script with nohup ./startscript.sh & so it does not terminate if you close the console.

share|improve this answer
Please accept your answer if it solved your problem. –  terdon Dec 2 '12 at 16:33
thanks for the reminder, I wasn't able to accept it immediately because I hadn't got enough reputation :P –  le_me Dec 2 '12 at 19:24
add comment

Your Answer


