Questions tagged [lock]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
216
votes
9answers
626k views

How to get over “device or resource busy”?

I tried to rm -rf a folder, and got "device or resource busy". In Windows, I would have used LockHunter to resolve this. What's the linux equivalent? (Please give as answer a simple "unlock this" ...
71
votes
3answers
80k views

What are pid and lock files for?

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 ...
64
votes
13answers
56k views

Correct locking in shell scripts?

Sometimes you have to make sure that only one instance of a shell script is running at the same time. For example a cron job which is executed via crond that does not provide locking on its own (e.g. ...
50
votes
4answers
157k views

How to list processes locking file?

Using flock, several processes can have a shared lock at the same time, or be waiting to acquire a write lock. How do I get a list of these processes? That is, for a given file X, ideally to find the ...
30
votes
5answers
48k views

What is a spinlock in Linux?

I would like to know about Linux spinlocks in detail; could someone explain them to me?
30
votes
2answers
10k views

Does redirecting output to a file apply a lock on the file?

If I have a command $ ./script >> file.log that gets called twice, with the second call occurring before the first one ends, what happens? Does the first call get an exclusive lock on the ...
29
votes
6answers
26k views

Gedit won't save a file on a VirtualBox share: Text file busy

I have a text file that I can change using other applications (for example openoffice). But when I try to change and save it using gedit, I am getting error from gedit: Could not save the file /...
26
votes
11answers
21k views

How to make sure only one instance of a bash script runs?

A solution that does not require additional tools would be prefered.
21
votes
3answers
2k views

How does this script ensure that only one instance of itself is running?

On 19 Aug 2013, Randal L. Schwartz posted this shell script, which was intended to ensure, on Linux, "that only one instance of [the] script is running, without race conditions or having to clean up ...
15
votes
3answers
34k views

what is the difference between spin locks and semaphores?

What are the basic differences between spin locks and semaphores in action?
13
votes
2answers
3k views

Is flock & exec safe in bash?

The "standard" locking snippet I've seen goes something like... ( flock -n 200 || exit 1; # do stuff ) 200>program.lock Is it safe (testing seems to say so) to use exec at that point? ...
12
votes
2answers
21k views

How to lock my session in Xfce?

I am looking a simple way to lock my session in Xfce (Debian Unstable). I don't want to have to write my password at every wake-up but I want to be able to press to a shortcut (which launches a ...
11
votes
2answers
94k views

How to lock users after 5 unsuccessful login tries?

How to lock users after 5 unsuccessful login tries? I gathered a few distributions/versions to how to do it, but I can't test it. RHEL4: by adding the: auth required /lib/security/$ISA/...
11
votes
3answers
6k views

Pass multiple commands to flock

flock -x -w 5 ~/counter.txt 'COUNTER=$(cat ~/counter.txt); echo $((COUNTER + 1)) > ~/counter.txt' How would I pass multiple commands to flock as in the example above? As far as I understand, ...
9
votes
3answers
1k views

Why do package managers need lock files?

dpkg uses a lock file (/var/lib/dpkg/lock), when in use. Why are these lockfiles needed? Why are multiple instances not possible?
9
votes
2answers
2k views

How can I tell when a process has finished writing to a file?

I have a process which has been spawned from a shell. It is running as a background process and exporting a DB to a CSV file in /tmp. How can I tell when the background process has completed (...
8
votes
3answers
23k views

Obtain exclusive read/write lock on a file for atomic updates

I want to have a file that is used as a counter. User A will write and increment this number, while User B requests to read the file. Is it possible that User A can lock this file so no one can read ...
8
votes
1answer
662 views

RasPi - USB microphone locks up

On my RasPi board, Debian Linux, the USB microphone occasionally gets locked up such that nothing can use it. The microphone has a LED which is usually flashing, when it's locked, it turns off. The ...
7
votes
2answers
20k views

How do you make a cross-process locking in Linux (C/C++)?

I need to make sure one process executes only in one instance at a time. On Windows you could use named mutex. But I have no idea what to use on Linux. I think I've seen an approach were app creates ...
6
votes
1answer
953 views

How to make sure only one instance accessing the file at a time in a folder?

I have a shell script which will be executed by multiple instances and if an instance accessing a file and doing some operation how can I make sure other instances are not accessing the same file and ...
6
votes
1answer
3k views

What is advisory locking on files that Unix systems typically employs?

I read that Unix like system typically use advisory locking for file IO vs mandatory locking used by Microsoft OS's. If I understood correctly in mandatory locking file lock is enforced by the OS ...
6
votes
1answer
1k views

Is `ln` atomic and reliable on NFS? Could NFS replace GFS in this use case?

I have a cluster with a bunch of servers with a shared disk containing a GFS global file system that all nodes access simultaneously. Each node in the cluster run the same program (a shell script is ...
5
votes
1answer
4k views

Why flock doesn't clean the lock file? [closed]

After process is completed, I see that the lock file isn't deleted? Is there any reason that why flock keeps the file ? Also how does flock knows if there is a lock acquired ? Here is the example ...
5
votes
2answers
2k views

file corresponds to /proc/locks

$ cat /proc/locks 1: POSIX ADVISORY WRITE 458 03:07:133880 0 EOF 2: FLOCK ADVISORY WRITE 404 03:07:133879 0 EOF The fields are: ordinal number(1), type(2), mode(3), type(4), pid(5), maj:min:inode(6)...
4
votes
2answers
2k views

Linux file hierarchy - what's the best location to store lockfiles?

I want to synchronize processes based on lock files (/ socket files). These files should only be removable by their creator user. There are plenty of choices: /dev/shm /var/lock /run/lock /run/...
4
votes
3answers
4k views

How to lock on Solaris 10?

On Linux I use flock lock command to execute a command with an exclusive lock. What is the standard operating system command of Solaris 10 to do the same in a shell?
4
votes
1answer
16k views

NFS file locking not working, am I misunderstanding?

I'm planning on having a complicated file sharing setup, and want to make sure I don't destroy file locking. (Wanting to use bind mounting, nfs, nfs over rdma (InfiniBand file sharing), and virtfs (...
4
votes
2answers
27k views

Monitoring file locks, locked using flock

I would like to get a list of pid's which hold shared lock on /tmp/file. Is this possible using simple command line tools?
4
votes
1answer
1k views

xautolock: how to auto-unlock on user activity?

I need to run a script after a period of user inactivity. For that I use the wonderful program xautolock. Now, I also need to run another script when the activity is resumed (e.g. mouse is moved or a ...
4
votes
1answer
4k views

Lock CD/DVD drive (prevent eject) [duplicate]

On my laptop the eject button for the CD/DVD drive is placed in a very unfortunate position and I hit it a dozen times per day and the tray pops out. I found many solutions which seemed to work for ...
3
votes
1answer
2k views

Lock file from being edited or deleted

I have CentOS and cPanel installed. I want to lock certain files (e.g. .ftpquota) from being deleted through FTP. I tried to change the ownership to root:root or root:lock but it didn't work; neither ...
3
votes
2answers
9k views

How do I kill the process holding the apt lock?

I tried doing sudo apt update but got: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable) E: Unable to lock directory /var/lib/apt/lists/ I am trying ...
3
votes
1answer
14k views

NFS mount: Device or resource busy

I referred the following link, the solution works. How to get over "device or resource busy"? The above solution works when you are manually deleting the file. But I have a python script ...
3
votes
2answers
738 views

How /var/lib/dpkg/lock works?

/var/lib/dpkg/lock is file that holding a lock when "A package manager is working". But how this system works? I have /var/lib/dpkg/lock everytime when I have Linux working. When I use one of package ...
3
votes
1answer
14k views

Are lock, mutex, and semaphore for between threads or between processes?

I think lock, mutex, semaphore are used for synchronized multiple (threads or processes?) to access something simultaneously. Must this "something" be some shared memory between multiple (threads or ...
3
votes
1answer
292 views

Locking in a shell script

I want to write a long-running shell script so that only one copy could be run at a time. If the script crashes, I want a new invocation of the script not to be stopped by a lock from the crashed ...
3
votes
1answer
501 views

Bash, fork with CLOEXEC

Can Bash execute a subprocess while preventing a subprocess from inheriting a file descriptor? if flock -nx 9 then # If begin program runs away, it will keep the lock. begin program else echo "...
3
votes
1answer
1k views

Controlling bash script concurrency, flock inheritance

I have a bash deployment script that handles deploying updated code to a Tomcat instance on CentOS, however, both Chef and RunDeck may invoke the script, and since Chef runs periodically there is a ...
3
votes
2answers
863 views

Can I safely read a file that is appended to by another process?

If process A copies files to some location loc and process B regularly copies the files from loc to some other location, can B read a file that is currently in the process of being copied to loc by A? ...
3
votes
1answer
860 views

How to use lockstat.txt on Ubuntu 12.04?

I have Ubuntu 12.04 installed on my machine. I want to use lockstat. I have looked at Documentation/lockstat.txt but I am not getting how to use it, if possible please help me out. I am working on a ...
3
votes
1answer
69 views

Lock Virtual Terminals when resuming

Many screen lockers (mine is i3lock) do not block access to other Virtual Terminals. This means that, if I leave a session opened in some VT, then even when the desktop is locked (for example when ...
3
votes
1answer
633 views

Check and Test Lock from other Process

I am trying to create a service wrapper (init.d script) around one of my favorite applications. The application creates both a PID and a lock file, and so I'm trying to use those to ensure that I can ...
3
votes
1answer
229 views

XScreenSaver: 1-2 seconds delay within which my laptop isn't locked

I want my screen to lock automatically and I'm using XScreenSaver. It locks the screen when my laptop suspends but when open the lid or press the power button, there's a small, 1-2 seconds delay and ...
2
votes
3answers
3k views

Does killing a process close all files opened by that process?

I want to kill a process which has an open connection to a database. If I kill the process, can I make sure that the database is unlocked?
2
votes
1answer
1k views

Does linux have file locking protection when trying to rename/deleting files

I have a cron job that kicks off a new process every day. The process runs every 5 minutes and appends to a log file. Another cron job runs every 60 minutes. It takes some of the data in the log file,...
2
votes
1answer
3k views

Generic shared resource locking techniques - is flock the solution?

I need to implement a locking scheme so that multiple processes can share a set of resources, while a "special" process can obtain exclusive access to that set of resources. These are batch ...
2
votes
1answer
121 views

Is pseudo terminals ( unlockpt / TIOCSPTLCK ) a security feature?

After having opened the master part of a pseude-terminal int fd_pseudo_term_master = open("/dev/ptmx",O_RDWR); there is the file /dev/pts/[NUMBER] created, representing the slave part of he pseudo-...
2
votes
1answer
819 views

Does linux tar block write access to files

I have a large (>10GB) folder full of images on a live webserver that I need to back up and transfer. I'm worried that if I tar the folder, the files will be blocked for reading by the webserver, ...
2
votes
2answers
140 views

Lock file naming pattern

Whenever I open a LibreOffice document, LibreOffice creates a lock file along the original file. This file has a naming schema like the following: .~lock.MyDocument.odt# Is this a LibreOffice ...
2
votes
1answer
7k views

How to do locking a file from a shell script? [duplicate]

I would like to synchronize two Bash scripts via a file lock. How to do this? Zsh has zsystem flock call, but is there a more "shellish" method that's also available to Bash?