22
votes
How to get over "device or resource busy"?
I experience this frequently on servers that have NFS network file systems. I am assuming it has something to do with the filesystem, since the files are typically named like .nfs000000123089abcxyz.
...
22
votes
How does this script ensure that only one instance of itself is running?
Why does the script need to redirect, to a file descriptor inherited by the subshell, a copy of its own contents rather than, say, the contents of some other file?
You could use any file, as long as ...
18
votes
How to make sure only one instance of a bash script runs?
One other way to make sure a single instance of bash script runs:
#! /bin/bash -
# Check if another instance of script is running
if pidof -o %PPID -x -- "$0" >/dev/null; then
printf &...
15
votes
How to get over "device or resource busy"?
Riffing off of Prabhat's answer, I had this issue in macos high sierra when I stranded an encfs process, rebooting solved it, but this
ps -ef | grep name-of-busy-dir
Showed me the process and the PID ...
12
votes
Handling of stale file locks in Linux and robust usage of flock
An flock lock is associated with a file description object; it will go away once all file descriptors referring to the file description have been closed (see the flock.2 manpage).
If the file is still ...
11
votes
apt-get wait for lock release
(Repasting from Ask ubuntu)
There is now an option built into apt-get that lets you wait for the other apt to finish.
Use the DPkg::Lock::Timeout option to set a timeout, in seconds, for an apt-get ...
11
votes
How do I kill the process holding the apt lock?
If you want to kill processes based on their name or argument list, use pkill.
pkill regexp
Will kill all processes whose name matches the regexp extended regular expression.
pkill -f regexp
Will ...
11
votes
How does this script ensure that only one instance of itself is running?
A file lock is attached to a file, through a file description. At a high level, the sequence of operations in one instance of the script is:
Open the file to which the lock is attached (“the lock file”...
8
votes
Accepted
Lock a bash script based on parameter?
A tool such as flock can help manage locks. (It may not work with NFS, depending on whether you believe the documentation or the practice, and similarly may or may not work on SMB or indeed any other ...
6
votes
How do I kill the process holding the apt lock?
This illustrates two reasons why you shouldn't use ps … | grep ….
ps prints a title line. But since the output is piped into grep, and the grep pattern doesn't match the title line, you don't get to ...
6
votes
Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
Your apt-get install --reinstall command doesn't have a sudo in front of it.
A command such as sudo x && y only runs the x command under sudo; the y command runs with normal user privileges.
...
5
votes
Accepted
Is pseudo terminals ( unlockpt / TIOCSPTLCK ) a security feature?
The old AT&T System 5 mechanism for pseudo-terminal slave devices was that they were ordinary persistent character device nodes under /dev. There was a multiplexor master device at /dev/ptmx. ...
5
votes
How to make sure only one instance of a bash script runs?
This is a modified version of Anselmo's Answer. The idea is to create a read only file descriptor using the bash script itself and use flock to handle the lock.
script=`realpath $0` # get absolute ...
5
votes
How to list processes locking file?
in case lsof itself is missing on the system, ls /proc/*/fd/* | grep LOCK_FILE_NAME should provide the same information.
5
votes
Arch linux: i3wm set lock time out (xss-lock i3lock)
A way of achieving it is by setting the screen timeout with xset.
xset dpms 0 0 300
300 is equal to five minutes.
7200 for two hours.
You can add the following to the auto start section on i3 config ...
5
votes
Code works in bash, not in zsh -- flock on arbitrary fd
Don’t hardcode the fd. Let the shell acquire it for you:
(
typeset fd=
exec {fd}> /tmp/blah.lockfile
flock -e $fd
echo "In critical section"
sleep 5
)
echo "After critical ...
4
votes
Bash, fork with CLOEXEC
With bash 5.0+ and the example loadable modules (debian requires you also install the bash-builtins package), you can use the fdflags loadable module.
if flock -nx 9
then
enable -f /usr/lib/bash/...
4
votes
How does this script ensure that only one instance of itself is running?
The file used for locking is unimportant, the script uses $0 because that's a file that is known to exist.
The order in which the locks are obtained will be more or less random, depending on how fast ...
4
votes
Handling of stale file locks in Linux and robust usage of flock
I ran into this same problem with flock. thejh's suggestion to use fuser helped me track down the problem. It turned out that the command I ran with flock launched a child process that remained in the ...
4
votes
Accepted
laptop keyboard locks up after closing lid
Alright, so.
In a terminal, open /etc/default/grub.
Find the line that starts with GRUB_CMDLINE_LINUX_DEFAULT=.
Replace it with GRUB_CMDLINE_LINUX_DEFAULT="atkbd.reset i8042.reset i8042.nomux ...
3
votes
Accepted
Where is official documentation about locking mechanisms for etckeeper, apt, and/or dpkg?
The cron job is treating /var/cache/etckeeper/packagelist.pre-install as evidence that an installation is being processed, so it shouldn’t archive anything just yet. That file isn’t supposed to be a ...
3
votes
some other process automatically locks /var/lib/dpkg/lock for few minutes when restarting my system.
See what process has the file open with lsof /var/lib/dpkg/lock or fuser /var/lib/dpkg/lock.
Chances are that it's the apt-daily service, which normally runs once a day to run apt-get update and ...
3
votes
What is a spinlock in Linux?
A spinlock is a lock that operates by disabling scheduler and possibly interrupts (irqsave variant) on that particular core that the lock is acquired on. It is different from a mutex in that it ...
3
votes
Can we tell if a command is being run by a process or not, by looking at the flock lock file alone?
Yes. It is explicitly stated in man 1 flock that flock command uses flock() and man 2 flock notes section says that flock() is implemented by system call since Linux kernel 2.0.
3
votes
Accepted
Lock file naming pattern
The leading dot hides the file from some directory listings. This comes from historical behavior of the ls command, which lead many programs to use leading dots to denote files that aren't meant to be ...
3
votes
file access blocked by a lock - how to remove?
repository is already locked by PID 30546 on ANB by tomc
You should first use ps -fp 30546 to see if the process that locked the repository still exists. Since the lock is already 509h old according ...
3
votes
Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
It worked with the below one on Kali Linux
sudo apt-get update && sudo apt-get upgrade -y
3
votes
Accepted
How To enable/disable "Lock Screen" setting from Linux terminal?
Unfortunately I cannot give you a straight answer but might guide you to it.
Essentially you are looking for the place where Xfce stores its settings and then a way to change them (one setting ...
3
votes
file locking mandated by OS/filesystem rather than opted into by file accessors?
No, FreeBSD does not support mandatory locking, neither do most other Unixish systems.
Linux used to have some support for that, but it was ripped out in v5.14. (One problem with it being that any ...
2
votes
Correct locking in shell scripts?
For actual use, you should use the top voted answer.
However, I want to discuss some various broken and semi-workable approaches using ps and the many caveats they have, since I keep seeing people use ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
lock × 144files × 26
linux × 20
shell-script × 16
bash × 12
debian × 9
nfs × 9
process × 8
screen-lock × 8
concurrency × 8
flock × 8
apt × 7
dpkg × 7
shell × 6
filesystems × 5
suspend × 5
ubuntu × 4
arch-linux × 4
i3 × 4
spinlock × 4
terminal × 3
solaris × 3
tty × 3
manjaro × 3
freeze × 3