Link to home
Start Free TrialLog in
Avatar of Tech_20
Tech_20

asked on

Ruby error "undefined method"

I am creating a short Ruby script to kill a stuck process. I checked the syntax (see attached script) and I get the error message below. Please help.

______________

killProcStk.rb:24:in `block in <main>': undefined method `*' for nil:NilClass (NoMethodError)
      from killProcStk.rb:21:in `each'
      from killProcStk.rb:21:in `<main>'
ruby_script.txt
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

I don't see any terminator for the do. Is this the full script? Please use the CODE button to insert the full script in-line
Avatar of Tech_20
Tech_20

ASKER

Yes this is the full script. I was looking for a block of script to write to factor in if there is no stuck process and the current value is 'nil'. I believe that issue is the source of the posted error message.
Here's your script where we can all see it
#!/usr/bin/ruby

max_time = 300

ps_list = `ps -eo cputime -eo pcpu -eo pid -eo user -eo command`

list = ps_list.split(/\n/)

list.each do |p|
    process = p.split
    process[0] =~ /(\d+):(\d+):(\d+)/
    cpu_time = $1*3600 + $2*60 + $3
    next if cpu_time < $max_time
    next if process[3] == "root" or process[3] == "postfix" 
    next if process[4] == "kdeinit" 
    
    begin
        print "Would you like to kill: #{process[4]} (y/n)? "
        if gets.downcase == "y"
            Process.kill :TERM,process[2]
        end
        rescue
            puts "Couldn't kill the process...check Linux permissions."
                retry
        end
    end

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tech_20

ASKER

Thank you, Duncan. This worked!!
Avatar of Tech_20

ASKER

Thank you, Duncan. You nailed it!!