Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:22:26+00:00 2026-06-15T15:22:26+00:00

I have a Ruby script that needs to run about one time a second.

  • 0

I have a Ruby script that needs to run about one time a second. I am using a Ruby script to keep track of modifications of files in a directory and want the script to track updates in “live” time.

Basically, I want my script to do the same kind of thing as running “top” on a Unix shell, where the screen is updated every second or so. Is there an equivalent to setInterval in Ruby like there is in JavaScript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:22:27+00:00Added an answer on June 15, 2026 at 3:22 pm

    There are a few ways to do this.

    The quick-and-dirty versions:

    1. shell (kornish):

      while :; do
         my_ruby_script.rb
         sleep 1
      done
      
    2. watch(1):

      shell$ watch -n 1 my_ruby_script.rb
      

      This will run your script every second and keep the output of the most recent run displayed in your terminal.

    3. in ruby:

      while true
         do_my_stuff
         sleep 1
      end
      

    These all suffer from the same issue: if the actual script/function takes time to run, it makes the loop run less than every second.

    Here is a ruby function that will make sure the function is called (almost) exactly every second, as long as the function doesn’t take longer than a second:

    def secondly_loop
        last = Time.now
        while true
            yield
            now = Time.now
            _next = [last + 1,now].max
            sleep (_next-now)
            last = _next
        end
    end
    

    Use it like this:

    secondly_loop { my_function }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created one ruby script that I want to run with some flags
I have a specific script written in Ruby that needs root privileges. Most of
I'm trying to run a Ruby script using rbenv with cron. I know that
I have Ruby script that creates a proxy so that I can make HTTP
so i have ruby script that simply writes to a test.txt file with hello
I have a ruby script that opens a browser. I have it set to
I have a ruby script that does a few perforce operations (through the scripting
I have a ruby script that is simultaneously and asynchronously receiving and displaying messages
I have this ruby script that allows me to enter the name of a
I have a simple Ruby script that uses the rb-appscript gem to control iTunes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.