```
#!/usr/bin/env ruby
time_to_wait = ARGV[0].to_i
loop do
display_attached = `system_profiler SPDisplaysDataType | grep "Thunderbolt Display"`.chomp.length > 0
if display_attached
puts "#{Time.now} - Detected external display: instructing computer to not idle-sleep to avoid crashing, for #{time_to_wait} seconds"
`caffeinate -i -t #{time_to_wait}`
else
puts "#{Time.now} - No display detected. Sleeping for #{time_to_wait} seconds"
sleep time_to_wait
end
end
```