What are pid and lock files for?
Class | Daemons::PidFile |
In: | lib/daemons/pidfile.rb |
Parent: | Pid |
What is a Pid-File?
A Pid-File is a file containing the process identification number (pid) that is stored in a well-defined location of the filesystem thus allowing other programs to find out the pid of a running script.
Daemons needs the pid of the scripts that are currently running in the background to send them so called signals. Daemons uses the TERM signal to tell the script to exit when you issue a stop command.
How does a Pid-File look like?
Pid-Files generated by Daemons have to following format:
<scriptname>.rb<number>.pid
(Note that <number> is omitted if only one instance of the script can run at any time)
Each file just contains one line with the pid as string (for example 6432).
Where are the Pid-Files stored?
Daemons is configurable to store the Pid-Files relative to three different locations:
- in a directory relative to the directory where the script (the one that is supposed to run as a daemon) resides (:script option for :dir_mode)
- in a directory given by :dir (:normal option for :dir_mode)
- in the preconfigured directory /var/run (:system option for :dir_mode)
Attributes
dir | [R] | |
multiple | [R] | |
number | [R] | |
progname | [R] |
Public Class methods
# File lib/daemons/pidfile.rb, line 55 55: def PidFile.existing(path) 56: new_instance = PidFile.allocate 57: 58: new_instance.instance_variable_set(:@path, path) 59: 60: def new_instance.filename 61: return @path 62: end 63: 64: return new_instance 65: end
# File lib/daemons/pidfile.rb, line 36 36: def PidFile.find_files(dir, progname, delete = false) 37: files = Dir[File.join(dir, "#{progname}*.pid")] 38: 39: files.delete_if {|f| not (File.file?(f) and File.readable?(f))} 40: if delete 41: files.delete_if do |f| 42: pid = File.open(f) {|h| h.read}.to_i 43: rsl = ! Pid.running?(pid) 44: if rsl 45: puts "pid-file for killed process #{pid} found (#{f}), deleting." 46: begin; File.unlink(f); rescue ::Exception; end 47: end 48: rsl 49: end 50: end 51: 52: return files 53: end
# File lib/daemons/pidfile.rb, line 67 67: def initializeinitialize(dir, progname, multiple = false) 68: @dir = File.expand_path(dir) 69: @progname = progname 70: @multiple = multiple 71: @number = nil 72: @number = 0 if multiple 73: 74: if multiple 75: while File.exist?(filename) and @number < 1024 76: @number += 1 77: end 78: 79: if @number == 1024 80: raise RuntimeException('cannot run more than 1024 instances of the application') 81: end 82: end 83: end
Public Instance methods
# File lib/daemons/pidfile.rb, line 100 100: def cleanup 101: File.delete(filename) if pid == Process.pid 102: end
# File lib/daemons/pidfile.rb, line 85 85: def filename 86: File.join(@dir, "#{@progname}#{ @number or '' }.pid") 87: end
feedback
|
migrated from serverfault.com May 7 '11 at 20:41
This question came from our site for system administrators and desktop support professionals.
pid files are written by some programs to record their process ID while they are starting. This has multiple purposes:
Mere presence of a pid file doesn't guarantee that that particular process id is running, of course, so this method isn't 100% foolproof but "good enough" in a lot of instances. Checking if a particular PID exists in the process table isn't totally portable across UNIX-like operating systems unless you want to depend on the Lock files are used by programs to ensure two (well-behaved) separate instances of a program, which may be running concurrently on one system, don't access something else at the same time. The idea is before the program accesses its resource, it checks for presence of a lock file, and if the lock file exists, either error out or wait for it to go away. When it doesn't exist, the program wanting to "acquire" the resource creates the file, and then other instances that might come across later will wait for this process to be done with it. Of course, this assumes the program "acquiring" the lock does in fact release it and doesn't forget to delete the lock file. This works because the filesystem under all UNIX-like operating systems enforces serialization, which means only one change to the filesystem actually happens at any given time. Sort of like locks with databases and such. |
|||
feedback
|
These files are often used by daemons that should only be run once on a system. The PID file usually contains the process ID number of the already launched and running program if one exists. Also, when it starts up, it creates the lock file. As long as the lock file exists, it won't start another one without user intervention. If the lock file exists and the process id mentioned in the pid file isn't running, the daemon is considered to be in a "dead" state, meaning it's supposed to be running but isn't probably due to a crash or improper shutdown. This might initiate a special startup / restart scenario for some programs. Properly shutting it down will remove the lock file. |
|||
feedback
|
A PID file will contain the Process ID of a running process. This has various uses; you can read it and check that the process is still running and take appropriate action or read it and kill the process. A lock file is most likely application specific. Lock files are used to indicate that some resource is in use and that the process wanting access should wait until the resource is freed before continuing. |
posted on 2012-03-31 15:02 Richard.FreeBSD 阅读(266) 评论(0) 编辑 收藏 举报