Skip to content

Instantly share code, notes, and snippets.

@hiroara
Created May 2, 2015 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroara/5e28799a5199986fef1c to your computer and use it in GitHub Desktop.
Save hiroara/5e28799a5199986fef1c to your computer and use it in GitHub Desktop.
Watching Process and send YO
target_pid = ARGV[0].to_i
target_yo_user = ARGV[1].upcase
threshold_cpu = (ARGV[2] || 50).to_i
sampling_times = (ARGV[3] || 3).to_i
sleep_sec = (ARGV[4] || 1).to_i
puts "watching #{target_pid}..."
puts " - Target YO User: #{target_yo_user}"
puts " - Threshold of CPU Usage: #{threshold_cpu}%"
puts " - Sampling times: #{sampling_times}"
puts " - Sleep Seconds: #{sleep_sec}s"
loop do
sampling_times.times.map do
`ps o pid,%cpu,command`.split("\n").drop(1).map do |line|
pid, cpu, command = line.split(' ', 3)
{ pid: pid.to_i, cpu: cpu.to_f, command: command }
end.find{ |process| process[:pid] == target_pid }.tap{ sleep sleep_sec }
end.compact.tap do |processes|
if !processes.empty? && processes.map{ |process| process[:cpu] }.inject(0, :+) / sampling_times > threshold_cpu
puts "Sleeping... CPU (average of #{sampling_times} times): #{processes.first[:cpu]}%"
else
puts "Now sending YO!"
`curl -s -X POST "http://api.justyo.co/yo/?api_token=#{ENV["YO_API_TOKEN"]}&username=#{target_yo_user}"`
exit 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment