Skip to content

Instantly share code, notes, and snippets.

@technicalpickles
Created November 7, 2017 20:56
Show Gist options
  • Save technicalpickles/5dfbca3de1d0ffbd1b0469c289f424c5 to your computer and use it in GitHub Desktop.
Save technicalpickles/5dfbca3de1d0ffbd1b0469c289f424c5 to your computer and use it in GitHub Desktop.
Find out what is keeping your Mac display on
#!/usr/bin/env ruby
assertions = {}
lines = `pmset -g assertions`.lines.map {|line| line.chomp }
lines.each do |line|
# pid 329(Slack): [0x00004c1d00058b13] 24:48:54 NoDisplaySleepAssertion named: "Electron"
if line =~ /pid (\d+)\((.*)\): \[.*\] (\d+:\d+:\d+) (\w+) named: \"(.*)\"/
pid = $1.to_i
application = $2
date = $3
assertion_type = $4
name = $5
# binding.pry
assertions[assertion_type] ||= []
assertions[assertion_type] << {
pid: pid,
application: application,
date: date,
name: name
}
end
end
count = (assertions["NoDisplaySleepAssertion"] || []).size
if count > 0
puts "ugh, of course it's #{count == 1 ? 'this' : 'these'} joker#{'s' if count > 1}:"
assertions["NoDisplaySleepAssertion"].each do |process|
puts "#{process[:application]} (#{process[:pid]})"
end
else
puts "Dunno, seems like I can sleep"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment