Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-14-2007, 04:10 PM   #1
Registered: Sep 2006
Posts: 57

Rep: Reputation: 15
newbie: pid file vs lock file

What is the purpose of pid file? Lock file used to lock a file so that other programs prevented from using it?

Lock file

Pid file

I need to do this:

1) run a program by cron and want to prevent cron from starting another instance of the same program if it's already running.

2) I need to scp the file to remote machine (com2) and lock the file in comp2 while ProgramA on comp1 move the file from comp1 to com2. So during scping of file, Program B on comp2 doesn't access the file that is being secure copied.

Can someone suggest what I need to do?

thanks in advance.
Old 12-14-2007, 10:39 PM   #2
Senior Member
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
Every process gets a process ID (pid) number. The system keeps track of processes by their pid. You can also control processes by their pid... kill 2134, or some-such.

Some programs use lock files to prevent other instances of the program running, or to have exclusive access to other files. yum and apt do, so that a second instance doesn't start up and the two mess up the system by both installing new packages at the same time.

A lock file isn't an actual lock on a file, it's just a file in a certain location that the programs are programmed to check. If file this_is_my_lock_file exists, do something. Any scripting language you use will be able to create and check for the existence of a file by name. Use of a lock file has to be written into a program. If program A creates a lock file on computer 2, but program B isn't programmed to check for the lock file, you might as well not have ever created it.

It might be better to put the scp-ing program on comp2... Program A on comp 2 checks for a lock file. If it finds one it exits. This keeps two instances from running. If it doesn't find a lock file, it creates one, then calls to comp1 for a new file. If there is no new file, it deletes the lock file then exits. If there is a new file, it copies it over to a temp file and closes the connection. It then creates a second lock file. This second lock file is the one program B should check for. With the second lock file in place, program A copies the temp file to where you want it, then deletes the temp file and both lock files, then exits.

By running program A on comp2, you have better control of the lock files. If program A was on comp1, and it creates a lock file on comp2, and the connection dies between them, then the lock file is still on comp2, and program B can't use the data file. If program A is on comp2 and the connection dies, program A can still access the lock file and delete it.

By using the temp file, if the connection dies and the whole file isn't copied over, you don't get stuck with half a data file on comp2. You might even want to have program A backup the file before replacing it with the temp file, just in case.

You might also want program B to create it's own lock file, so that program A won't try to replace the file while program B is currently using it. If program A finds program B's lock file, it can wait a short time and check again.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post file is missing ashishzankar Linux - Newbie 2 11-07-2007 11:19 AM
reading /proc/pid/stat file? sanjaykhuntia Linux - Enterprise 1 10-29-2007 02:45 AM
nmbd dead but pid file exists mammajamma101 Linux - Newbie 2 01-14-2005 09:16 AM
Can't read lock file /tmp/.X0-lock squinn Linux - Newbie 3 07-13-2004 03:42 PM
ERROR: Couldn't write pid to pid file lawrencegoodman Linux - Newbie 2 02-13-2004 08:05 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:11 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration