Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I often see that programs specify pid and lock files. And I'm not quite sure what they do.

For example, when compiling nginx:

--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \

Can somebody shed some light on this one?

share|improve this question

migrated from serverfault.com May 7 '11 at 20:41

This question came from our site for system and network administrators.

3 Answers 3

up vote 25 down vote accepted

pid files are written by some programs to record their process ID while they are starting. This has multiple purposes:

  • It's a signal to other processes and users of the system that that particular program is running, or at least started successfully.
  • It allows one to write a script really easy to check if it's running and issue a plain kill command if one wants to end it.
  • It's a cheap way for a program to see if a previous running instance of it did not exit successfully.

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 ps utility, which may not be desirable to call in all instances (and I believe some UNIX-like operating systems implement ps differently anyway).

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.

share|improve this answer
    
So if the program "acquiring" the lock crashes and doesn't delete the lock file, then the other program will never be able to access the resources. Is that right? –  CodeBlue Jun 10 at 15:16
1  
This is correct, unless the lock file is manually deleted. VMWare Player exhibits this behaivor, for example, if VMWare Player crashes, you have to delete a .lck file in the VM's directory otherwise it will tell you it's in use when you try to start it. –  ultrasawblade Jun 11 at 14:04

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.