Skip to content

Instantly share code, notes, and snippets.

@gamecreature
Created April 26, 2021 07:58
Show Gist options
  • Save gamecreature/1a0e384cbe1f03d2fc3666b5e2b0e5bc to your computer and use it in GitHub Desktop.
Save gamecreature/1a0e384cbe1f03d2fc3666b5e2b0e5bc to your computer and use it in GitHub Desktop.
# Use it by including it directly in the unix-ruby script
def run_script_once
emails = %w[info@example.com]
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
#!/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