Last active
January 22, 2016 03:39
-
-
Save pachacamac/814a74e082ae03f3c960 to your computer and use it in GitHub Desktop.
Turns Your Laptop into an Alarm Clock While Putting It into Suspend
This file contains hidden or 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
puts 'Make sure to run this with sudo-rights!' unless Process::UID.eid == 0 | |
parser = begin | |
require 'chronic' | |
lambda{|s| Chronic.parse s} | |
rescue LoadError | |
STDERR.puts 'Warning: Gem "Chronic" not found. Using Time.parse instead!' | |
require 'time' | |
lambda{|s| Time.parse s} | |
end | |
begin | |
at = parser[ARGV.join(' ')] | |
ads = (at - Time.now).to_i | |
rescue | |
STDERR.puts $! | |
puts "usage: #{__FILE__} <alarm time>" | |
exit | |
end | |
ad = "#{ads / 3600} hours #{ads % 3600 / 60} minutes" | |
print "Alarm will go off in #{ad}. OK? (Y/n) " | |
exit unless ['Y','y',''].include?(STDIN.gets.chomp) | |
if ''==`pidof -x mplayer vlc mpg123 mpg321` # TODO: expand list of potential players! | |
print "No music player seems to be running. Continue anyways? (Y/n) " | |
exit unless ['Y','y',''].include?(STDIN.gets.chomp) | |
end | |
print "Setting RTC wakealarm ... " | |
puts `sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm" && sh -c "echo #{at.to_i} > /sys/class/rtc/rtc0/wakealarm"` | |
print "Setting xscreensaver-command -lock ... " | |
puts `xscreensaver-command -lock` | |
print "Going into suspend ... " | |
puts `pm-suspend` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love this!