LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   newbie: pid file vs lock file (https://www.linuxquestions.org/questions/linux-newbie-8/newbie-pid-file-vs-lock-file-606839/)

jCash 12-14-2007 04:10 PM

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
/var/lock/subsys

Pid file
/var/run

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.

ranger_nemo 12-14-2007 10:39 PM

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.


All times are GMT -5. The time now is 12:48 AM.