exception for program running on ruby code

Discussion in 'Ruby' started by Ahmad Azizan, Nov 11, 2009.

  1. Ahmad Azizan

    Ahmad Azizan Guest

    Hello,

    I'm trying to get an output from /var/log/syslog for certain amount of
    time by executing %x[sudo tail -f /var/log/syslog]

    Is it possible to set a timer for the execution, and if the time end,
    the execution will end (like we do Ctrl+C) ?

    Thank you
    --
    Posted via http://www.ruby-forum.com/.
     
    Ahmad Azizan, Nov 11, 2009
    #1
    1. Advertising

  2. Ahmad Azizan

    Ahmad Azizan Guest

    Ahmad Azizan wrote:
    > Hello,
    >
    > I'm trying to get an output from /var/log/syslog for certain amount of
    > time by executing %x[sudo tail -f /var/log/syslog]
    >
    > Is it possible to set a timer for the execution, and if the time end,
    > the execution will end (like we do Ctrl+C) ?
    >
    > Thank you


    I think I've got the solution,

    #!/usr/bin/ruby

    require 'timeout'
    begin
    status = Timeout::timeout(10) do
    %x[sudo tail -f /var/log/messages]
    end
    rescue Timeout::Error
    puts 'execution expired'
    end

    This might do the job

    --
    Posted via http://www.ruby-forum.com/.
     
    Ahmad Azizan, Nov 11, 2009
    #2
    1. Advertising

  3. Ahmad Azizan wrote:
    > Ahmad Azizan wrote:
    >> Hello,
    >>
    >> I'm trying to get an output from /var/log/syslog for certain amount of
    >> time by executing %x[sudo tail -f /var/log/syslog]
    >>
    >> Is it possible to set a timer for the execution, and if the time end,
    >> the execution will end (like we do Ctrl+C) ?
    >>
    >> Thank you

    >
    > I think I've got the solution,
    >
    > #!/usr/bin/ruby
    >
    > require 'timeout'
    > begin
    > status = Timeout::timeout(10) do
    > %x[sudo tail -f /var/log/messages]
    > end
    > rescue Timeout::Error
    > puts 'execution expired'
    > end
    >
    > This might do the job
    >


    I don't think you will get a value in status that way. Try this:

    require 'timeout'

    output = []
    begin
    Timeout.timeout 5 do
    IO.popen("ping 192.168.1.1") do |pipe|
    while line = pipe.gets
    output << line
    end
    end
    end
    rescue TimeoutError
    end

    puts output


    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Nov 11, 2009
    #3
  4. Ahmad Azizan wrote:
    > Ahmad Azizan wrote:
    >> Hello,
    >>
    >> I'm trying to get an output from /var/log/syslog for certain amount of
    >> time by executing %x[sudo tail -f /var/log/syslog]
    >>
    >> Is it possible to set a timer for the execution, and if the time end,
    >> the execution will end (like we do Ctrl+C) ?
    >>
    >> Thank you

    >
    > I think I've got the solution,
    >
    > #!/usr/bin/ruby
    >
    > require 'timeout'
    > begin
    > status = Timeout::timeout(10) do
    > %x[sudo tail -f /var/log/messages]
    > end
    > rescue Timeout::Error
    > puts 'execution expired'
    > end
    >
    > This might do the job
    >


    I don't think you will get a value in status that way. Try this:

    require 'timeout'

    output = []
    begin
    Timeout.timeout 5 do
    IO.popen("ping 192.168.1.1") do |pipe|
    while line = pipe.gets
    output << line
    end
    end
    end
    rescue TimeoutError
    end

    puts output


    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Nov 11, 2009
    #4
  5. Ahmad Azizan

    Ahmad Azizan Guest

    Joel VanderWerf wrote:

    > I don't think you will get a value in status that way. Try this:
    >
    > require 'timeout'
    >
    > output = []
    > begin
    > Timeout.timeout 5 do
    > IO.popen("ping 192.168.1.1") do |pipe|
    > while line = pipe.gets
    > output << line
    > end
    > end
    > end
    > rescue TimeoutError
    > end
    >
    > puts output


    Thanks for the correction. Actually I'm running javascript interpreter
    program called spidermonkey and it does not require storing values.
    After finished it will generate a file. However sometimes if the
    javascript code is not properly coded, or with infinite loops, the
    javascript interpreter will just hang in there waiting. So I dont want
    that to happen. Need to get it stop if it tooks more than 10 seconds.

    I've tried using the timeout, and it worked. However, I've found out
    that the program's process still running in background, which in this
    case it did not terminate after the timeout. How can I stop the process
    after reaching the timeout?

    --
    Posted via http://www.ruby-forum.com/.
     
    Ahmad Azizan, Nov 11, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    478
    Walter Roberson
    Aug 12, 2005
  2. Lorenzo Bettini
    Replies:
    3
    Views:
    622
    Lorenzo Bettini
    Sep 24, 2005
  3. mahesh
    Replies:
    0
    Views:
    1,450
    mahesh
    Mar 8, 2007
  4. Alexander Mahone

    Running a program (gperf) inside another C program

    Alexander Mahone, May 26, 2008, in forum: C Programming
    Replies:
    4
    Views:
    411
    Flash Gordon
    May 26, 2008
  5. Maxx
    Replies:
    14
    Views:
    891
    Ben Bacarisse
    Apr 4, 2011
Loading...

Share This Page