Skip to content

Instantly share code, notes, and snippets.

@mmb
Last active Aug 29, 2015
Embed
What would you like to do?
When USB drives are mounted, check them for a script called plz_verify.sh and run it if found.
#!/usr/bin/env ruby
# When USB drives are mounted, check them for a script called plz_verify.sh
# and run it if found.
#
# symlink all USB volumes you want to check into ~/usb
# ln -s /Volumes/mmb ~/usb/mmb
require 'logger'
require 'set'
logger = Logger.new(STDOUT)
state = Set.new
while true do
# the extra stars are for symlinks
current = Set.new(Dir.glob("#{Dir.home}/usb/**/*/**/plz_verify.sh"))
gone = state - current
new = current - state
state = current
gone.each do |script|
logger.info("#{script} unmounted")
end
new.each do |script|
logger.info("running #{script}")
script_contents = File.read(script)
IO.popen('sh', 'w') do |io|
io.write(script_contents)
end
end
sleep 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment