问题描述:

I have about 5 or 6 ruby scripts I want to run, right after each other. These are all on my local machine (OS X) and won't be run on a server.

Each takes about 15 minutes to run, and I don't want to have to wait for each one to finish before running the others manually.

Without using something as heavy as delayed_job or some other queueing gem, how can I achieve this?

Or should I go through the hassle of setting up sidekiq or something else?

Thanks.

P.S. It would be nice to restart the script if one of them times out (I am doing web crawling, so keeping an HTTP connection open sometimes gives me issues) - which happens occasionally.

网友答案:

As Zabba said, writing a script to call your scripts is a good solution. You could write a simple Bash script for this, or use Ruby as shown below:

#!/usr/bin/ruby

SCRIPTS=%w{s1.rb s2.rb s3.rb}
MAX_RETRIES=3

SCRIPTS.each do |script|
  MAX_RETRIES.times do |n|
    system "ruby #{script}"
    break if $?.exitstatus == 0
  end
end

This uses the system call in Ruby, and $? is the Process::Status object that you can use to capture the exit codes of your scripts.

For this to work properly, you just need to make sure that your scripts return an exit code (using the exit command) of 0 when successful, or something non-zero, e.g. 1 on failure.

There are some obvious security concerns running system, so unless you're on your local machine set your permissions accordingly :)

网友答案:

Assuming that your ruby scripts return a non-zero error code when a failure occurs, your bash script could be as simple as the following:

#!/bin/bash

ruby script1.rb
if [[ $? -ne 0 ]]; then
    ... handle script1 errors here ...
fi

ruby script2.rb
if [[ $? -ne 0 ]]; then
    ... handle script2 errors here ...
fi

... etc, etc ...
网友答案:

just execute them all from the terminal on one line. This is almost certainly the lightest weight solution.

ruby script0.rb; ruby script1.rb; ruby script2.rb; ruby script3.rb

This won't restart a script if it times out, but you should probably be handling that in the script itself...

相关阅读:
Top