Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Last active January 22, 2016 03:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pachacamac/814a74e082ae03f3c960 to your computer and use it in GitHub Desktop.
Save pachacamac/814a74e082ae03f3c960 to your computer and use it in GitHub Desktop.
Turns Your Laptop into an Alarm Clock While Putting It into Suspend
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`
@im-grey
Copy link

im-grey commented Jan 22, 2016

I love this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment