Sponsored Content
Top Forums Shell Programming and Scripting How to check/compare lock file? Post 302902043 by cnamejj on Saturday 17th of May 2014 06:13:09 PM
Old 05-17-2014
Eons ago I ended up writing C code to wrap execution of arbitrary commands with lock management just for cases like this. Meaning, I needed a way to coordinate the use of various scripts, tools, etc... that didn't already have a means of serializing their runs.

A couple of years ago I re-wrote that program and a few others and put them up on Github for anyone to grab. But I don't think you need to bother looking for packages to compile for what you want anymore. I just checked and found a command called "flock" that's already installed on my Ubuntu system that does the same sorts of things. So check "man flock" and see if it does what you want.

The main feature I think you need is for your "lockfile" to actually use file locking calls to coordinate access. In other words, the mere existence of the lockfile isn't enough to stop execution. What should block execution of another instance of the command is if you can't establish an exclusive lock on the file. And even if the previous command died without removing the lockfile, everything will just work. In fact you don't even need to remove the lockfile at all.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

lock file!

I found a lock file like this lrwxrwxr-x 1 sskb apollo 16 Oct 22 22:00 lock -> hostname:2747 (pl. note that hostname is a number like 123.4.5.6) but this was not shown in the file manager eventhough I had selected to show the hidden files. I could not even read the... (4 Replies)
Discussion started by: sskb
4 Replies

2. UNIX for Dummies Questions & Answers

how to lock keyboard without using lock command

how can I lock my keyboard while I'm away from the computer without using lock command. What other commands gives me the option to lock keyboard device? thanks (7 Replies)
Discussion started by: dianayun
7 Replies

3. UNIX for Dummies Questions & Answers

Lock File

Hi, We have a lock file being created called lck8c0001 created in Unixware 2.1.2. This is locking a printer. According to some websites, 8c0001 relates to the device name. How does one link 8c0001 to those devices listed in the /dev folder? I have done a ps -lp for all printers and have... (4 Replies)
Discussion started by: canman
4 Replies

4. UNIX for Dummies Questions & Answers

Need Script to check file exist and compare

I need a script that will check for the existence of new files that FTP'd in the morning, results go to log file. The 2nd step is to compare the new file with the previous days file. If the new file size is 30% or more smaller in size then previous day this needs to also be sent to log. This... (1 Reply)
Discussion started by: rbknisely
1 Replies

5. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies

6. UNIX for Advanced & Expert Users

file lock

I have an Essbase installation on Solaris 10 and need to get the backups configured. Unfortunately several key files are locked and Essbase (OLAP application) is not releasing the locks when the Essbase or the applications within stop running. It appears I can use chmod to unlock the files but I... (0 Replies)
Discussion started by: JavaBrian
0 Replies

7. Red Hat

Security Question: Lock after invalid login, Session Lock and Required Minimum Password Length

Hello all, If anyone has time, I have a few questions: How do I do the following in Linux. We are using Red Hat and Oracle Enterprise Linux, which is based on Red Hat too. 1. How to lock the account after a few (like 3) invalid password attempts? 2. How do you lock a screen after 30... (1 Reply)
Discussion started by: nstarz
1 Replies

8. Shell Programming and Scripting

Check and compare disk space and email it

I am very new to Linux and learning to script. This is for one of my servers at work that I have to keep track off as far as disk space and how it is used. I have tried to go line by line but little things keep chewing me up. I would appreciate any and all help or advice, and Mutt is installed on... (3 Replies)
Discussion started by: sgtjkj
3 Replies

9. UNIX for Advanced & Expert Users

Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied

Hi all, I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...). I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message. Is there any way to simply get a return code... (3 Replies)
Discussion started by: Dedalus
3 Replies

10. Shell Programming and Scripting

Check for particular files and compare the file names

Hi, Below are the 2 files in directory /tmp: masterCSF242323.img indexCSF242323.img 1) I want to compare if both the number (242323) are same in both the files. If they are same print - Files matching, else print files do not match. 2) Also if only index file is present in that... (7 Replies)
Discussion started by: apatil65
7 Replies
DOTLOCKFILE(1)							 Cistron Utilities						    DOTLOCKFILE(1)

NAME
dotlockfile - Utility to manage lockfiles SYNOPSIS
/usr/bin/dotlockfile [-l [-r retries] |-u|-t|-c] [-p] [-m|lockfile] DESCRIPTION
dotlockfile is a command line utility to reliably create, test and remove lockfiles. It creates lockfiles reliably on local and NFS filesystems, because the crucial steps of testing for a preexisting lockfile and creating it are performed atomically by a single call to link(2). Manpage lockfile_create(3) describes the used algorithm. dotlockfile is installed with attribute SETGID mail and thus can also be used to lock and unlock mailboxes even if the mailspool directory is only writable by group mail. The name dotlockfile comes from the way mailboxes are locked for updates on a lot of UNIX systems. A lockfile is created with the same filename as the mailbox but with the string ".lock" appended. The names dotlock and lockfile were already taken - hence the name dotlockfile :). OPTIONS
-l Create a lockfile if no preexisting valid lockfile is found, else wait and retry according to option -r. This option is the default. A lockfile is treated as valid, o if it holds the process-id of a running process, o or if it does not hold any process-id and has been touched less than 5 minutes ago (timestamp is younger than 5 minutes). -r retries The number of times dotlockfile retries to acquire the lock if it failed the first time before giving up. The initial sleep after failing to acquire the lock is 5 seconds. After each retry the sleep intervall is increased incrementally by 5 seconds up to a maximum sleep of 60 seconds between tries. The default number of retries is 5. To try only once, use "-r 0". -u Remove a lockfile. -t Touch an existing lockfile (update the timestamp). Useful for lockfiles on NFS filesystems. For lockfiles on local filesystems the -p option is preferable. -c For debugging only: Check for the existence of a valid lockfile. Note: Testing for a preexisting lockfile and writing of the lockfile must be done by the same "dotlockfile -l" or "dotlockfile -m" command, else the lockfile creation cannot be reliable. -p Write the process-id of the calling process into the lockfile. Also when testing for an existing lockfile, check the contents for the process-id of a running process to verify if the lockfile is still valid. Obviously useful only for lockfiles on local filesystems. -m Lock or unlock the current users mailbox. The path to the mailbox is the default system mailspool directory (usually /var/mail) with the username as gotten from getpwuid() appended. If the environment variable $MAIL is set, that is used instead. Then the string ".lock" is appended to get the name of the actual lockfile. lockfile The lockfile to be created or removed. Must not be specified, if the -m option is in effect. RETURN VALUE
Zero on success, and non-zero on failure. For the -c option, sucess means that a valid lockfile is already present. When locking (the default, or with the -l option) dotlockfile returns the same values as the library function lockfile_create(3). Unlocking a non-existant lockfile is not an error. NOTES
The lockfile is created exactly as named on the command line. The extension ".lock" is not automatically appended. This utility is a lot like the lockfile(1) utility included with procmail, and the mutt_dotlock(1) utility included with mutt. However the command-line arguments differ, and so does the return status. It is believed, that dotlockfile is the most flexible implementation, since it automatically detects when it needs to use priviliges to lock a mailbox, and does it safely. The above mentioned lockfile_create(3) manpage is present in the liblockfile-dev package. BUGS
None known. SEE ALSO
lockfile_create(3), maillock(3) AUTHOR
Miquel van Smoorenburg, miquels@cistron.nl September 8, 2009 DOTLOCKFILE(1)
All times are GMT -4. The time now is 04:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy