def execute(double_process_check = true,auto_recover = true,&process)
info "start script(#{File.expand_path($0)})"
warn "ERROR_MAIL_TO not defined.if you want error mail automatically,set this value." unless Module.constants.include?("ERROR_MAIL_TO")
script_started_at = Time.now
begin
if double_process_check
pg_path = File.expand_path($0)
pg_name = File.basename(pg_path)
hash = Digest::MD5.hexdigest(pg_path)
pid_file = "/tmp/.#{pg_name}.#{hash}.pid"
debug pid_file
if File.exists?(pid_file)
pid = File.open(pid_file).read.chomp
pid_list = `ps -e | awk '{print $1}'`
if pid_list =~ /#{pid}/
warn "pid:#{pid} still running"
return nil
else
if auto_recover
warn "pid file still exists,but process does not found.so process continues"
else
raise "pid file still exists,but process does not found"
end
end
end
File.open(pid_file, "w"){|file|
file.write $$
}
end
return (yield process)
rescue => e
error e
send_error_mail(e)
ensure
File.delete(pid_file) if double_process_check
info "finish script (%1.3fsec)" % (Time.now - script_started_at)
end
end