0.0
No commit activity in last 3 years
No release in over 3 years
gem for automatic checking, creation and deletion pid-file of an application.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.16
~> 5.0
~> 10.0
 Project Readme

PidFileBlock

PidFileBlock - gem for easy use pid-files. The gem automatically creates and deletes pid-file for application.

Installation

Add this line to your application's Gemfile:

gem 'pid_file_block'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pid_file_block

Usage

Simple example

#!/usr/bin/env ruby

require 'pid_file_block'

pid_file_block = PidFileBlock.new(piddir: '/run', pidfile: 'example.pid')
begin
  pid_file_block.open do
    # Put your code here
  end
rescue PidFileBlock::ProcessExistsError
  # Another process running. Exit with error.
  exit 1
end

With signal handlers

In previous example pid-file will not be deleted after the program termination with command kill or Ctrl-C. You may define your signal handlers and use the release method in them:

#!/usr/bin/env ruby

require 'pid_file_block'

$pid_file_block = nil

def do_exit
  $pid_file_block.release if $pid_file_block
  exit 0
end

old_term = Signal.trap('TERM') do
  do_exit
end
old_int = Signal.trap('INT') do
  do_exit
end

$pid_file_block = PidFileBlock.new(piddir: '/run', pidfile: 'example.run')
begin
  $pid_file_block.open do
    # Put your code here
  end
rescue PidFileBlock::ProcessExistsError
  # Another process running. Exit with error.
  exit 1
end

Another way - use the PidFileBlock::Application

PidFileBlock::Application

#!/usr/bin/env ruby

require 'pid_file_block'
require 'pid_file_block/application'

PidFileBlock::Application.run(piddir: '/run', pidfile: 'example.pid')
  # Put your code here
end

Kill a process with pid file from another

#!/usr/bin/env ruby

require 'pid_file_block'

pid_file_block = PidFileBlock.new(piddir: '/run', pidfile: 'example.pid')
pid_flie_block.kill

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/askh/pid_file_block.

License

The gem is available as open source under the terms of the MIT License.