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 9211527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:19:04+00:00 2026-06-18T01:19:04+00:00

Is there a way to run a ruby script and while executing commands in

  • 0

Is there a way to run a ruby script and while executing commands in the script ,still respond to key stroke ?

I want to run a ruby script but be able to press “Space” and pause the script (after currently running command executes ), and then press “Space” again and resume the script .

My only idea (and im sure its a weird one) , is to open a new thread and wait for key strokes there ,
then when ill get a key stroke , set a stop_flag.
Only now it looks like i need to be checking this flag after each command to know when to stop.

  • 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-18T01:19:05+00:00Added an answer on June 18, 2026 at 1:19 am

    You can use a signal to turn debug output on and off at will, if you have a logger set up with appropriate code sprinkled throughout your script:

     pid = fork do
    
      # set up a logger
      require 'logger'
      log = Logger.new(STDOUT)
      log.level = Logger::INFO
    
      # toggle between INFO and DEBUG log levels on SIGUSR1
      trap(:SIGUSR1) do
        if log.level == Logger::DEBUG
          log.level = Logger::INFO
        else
          log.level = Logger::DEBUG
        end
      end
    
      # Main loop - increment a counter and occasionally print progress
      # as INFO level.  DEBUG level prints progress at every iteration.
      counter = 0
      loop do
        counter += 1
        exit if counter > 100
        log.debug "Counter is #{counter}"
        log.info "Counter is #{counter}" if counter % 10 == 0
        sleep 0.1
      end
    
    end
    
    # This makes sure that the signal sender process exits when the
    # child process exits - only needed here to make the example
    # terminate nicely.
    trap(:SIGCLD) do
      exit(0) if Process.wait(-1, Process::WNOHANG) == pid
    end
    
    # This is an example of sending a signal to another process.
    # Any process may signal another by pid.
    # This example uses a forking parent-child model because this
    # approach conveniently yields the child pid to the parent.
    loop do
      puts "Press ENTER to send SIGUSR1 to child"
      STDIN.gets
      Process.kill :SIGUSR1, pid
    end
    

    The forking and SIGCLD trapping is to make the example fit into one file; any process may send a signal to another.

    The code inside the fork block is your script. The script sets up a logger with a default log level of INFO, and a handler for the SIGUSR1 signal that toggles the logger between DEBUG and INFO levels.

    The stuff outside of the fork block is just an example of sending a signal to another process. Pressing ENTER will send the signal and alter the logging level of the other process.

    This works on POSIX systems, I have no idea about Windows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run a program from within my Ruby script, but I want
Is there any way to run a Python script in Windows XP without a
Is there some way to run 2 threads at the same time? I want
Is there a way to run the GCC preprocessor, but only for user-defined macros?
Is there a way to run processes from rails script/console in the background? I
I just created a script/run in my project in the following way: #!/usr/bin/env ruby
Is there a way to run Legacy Ruby on Rails version 1.2.5 app on
I want to run a thread-safe piece of script in Ruby that calls an
I have a problem. I want to run a ruby script from another ruby
Is there an easy way to tell if a ruby script is already running

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.