Created
May 24, 2016 10:46
-
-
Save kapcod/bab23bc254c3f3c98d173d328dcf0864 to your computer and use it in GitHub Desktop.
Track sleep of the computer with Mac OSX agent config for autoload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>ilya.track_sleep</string> | |
<key>Program</key> | |
<string>/Users/sa/track_sleep.rb</string> | |
<key>WorkingDirectory</key> | |
<string>/Users/sa</string> | |
<key>RunAtLoad</key> | |
<true/> | |
<key>KeepAlive</key> | |
<true/> | |
</dict> | |
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run this to load the agent | |
launchctl load ~/Library/LaunchAgents/ilya.track_sleep.plist | |
# run this to unload the agent and stop the process | |
launchctl unload ~/Library/LaunchAgents/ilya.track_sleep.plist | |
# to apply config change, must unload and load |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def log(str) | |
# will always create log file in current working directory | |
open('track_sleep.log','a'){|f| f.puts str } | |
end | |
now=Time.now | |
log "*** #{now.strftime("%Y-%m-%d %a %H:%M:%S")}: Started" | |
while true | |
now, past = Time.now, now | |
diff = (now-past).to_i | |
if diff>15*60 | |
prev, last = last, now | |
log "Work: #{prev.strftime("%Y-%m-%d %a %H:%M")} -> #{past.strftime("%H:%M")}" if prev && past>prev && past.day==prev.day | |
# this part might be important in case of progam restart, when it won't have previous time | |
log "Sleep: #{past.strftime("%Y-%m-%d %a %H:%M:%S")} -> #{now.strftime("%Y-%m-%d %a %H:%M:%S")} (#{diff/3600}:#{(diff%3600)/60}:#{diff%60})" | |
end | |
sleep 10 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment