Created
April 26, 2021 07:58
-
-
Save gamecreature/1a0e384cbe1f03d2fc3666b5e2b0e5bc to your computer and use it in GitHub Desktop.
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
# Use it by including it directly in the unix-ruby script | |
def run_script_once | |
emails = %w[[email protected]] | |
path = "#{$0}.pid" | |
if File.exists?(path) | |
pid = IO.read(path).to_i | |
begin | |
if Process.getpgid(pid) | |
puts "Already Running!" | |
hostname = `/bin/hostname` | |
emails.each do |email| | |
`/usr/bin/mail -s "WARNING: script #{path} on host #{hostname} already running" "#{email}" < /dev/null` | |
end | |
exit | |
end | |
rescue Errno::ESRCH | |
puts "** .pid file exists, but process isn't running, continue.. **" | |
end | |
end | |
at_exit { File.unlink(path) if File.exist?(path) } | |
IO.write(path, Process.pid.to_s) | |
end | |
run_script_once |
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
#!/usr/bin/env ruby | |
require "#{__dir__}/run_once" | |
max = 12 | |
delay = 10 | |
1.upto(max) do |idx| | |
puts "#{idx}/#{max} Waiting #{delay} seconds .." | |
sleep delay | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment