Questions tagged [flock]

Shell: The `flock` utility manages locks in scripts; C programming: flock() applies or removes an advisory lock on an open file.

flock is a command line utility to manage flock advisory locks - i.e., they're a convention instead of a hard restriction.

298 questions
134
votes
14 answers

What is the best way to ensure only one instance of a Bash script is running?

What is the simplest/best way to ensure only one instance of a given script is running - assuming it's Bash on Linux? At the moment I'm doing: ps -C script.name.sh > /dev/null 2>&1 || ./script.name.sh but it has several issues: it puts the check…
user80168
40
votes
8 answers

Mac OS X equivalent of Linux flock(1) command

Is there a flock command on Mac OS X that manages file lock? http://linux.die.net/man/1/flock
png
  • 5,990
  • 2
  • 25
  • 16
39
votes
1 answer

bash flock: Why 200?

Regarding that thread: bash flock: exit if can't acquire lock I'll appreciate if someone can explain to me what does the '200' stand for. I've read about flock and it seems that 200 if to specify a File Descriptor, but what is so good about this…
Subway
  • 5,286
  • 11
  • 48
  • 59
35
votes
3 answers

flock(): removing locked file without race condition?

I'm using flock() for inter-process named mutexes (i.e. some process can decide to hold a lock on "some_name", which is implemented by locking a file named "some_name" in a temp directory: lockfile = "/tmp/some_name.lock"; fd = open(lockfile,…
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
28
votes
1 answer

Linux flock, how to "just" lock a file?

In Bash, I'm trying to make a function getLock to be used with different lock names. function getLock { getLock_FILE="${1}" getLock_OP="${2}" case "${getLock_OP}" in "LOCK_UN") flock -u "${getLock_FILE}" …
JorgeeFG
  • 5,651
  • 12
  • 59
  • 92
20
votes
4 answers

Exclusively open a device file in Linux

What ways are there available, for exclusively opening a device file (say, the display frame buffer)? [Info: I already know about flock() & friends, which have an effect only when the other applications are also using it (in other words: open() will…
user2075654
  • 201
  • 2
  • 4
19
votes
3 answers

Running python script with cron only if not running

I need to run a python script (job.py) every minute. This script must not be started if it is already running. Its execution time can be between 10 seconds and several hours. So I put into my crontab: * * * * * root cd /home/lorenzo/cron && python…
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
18
votes
1 answer

python lockf and flock behaviour

I have read enough posts on stackoverflow regarding the difference between flock/lockf/fcntl but I am unable to answer the below observation: >>> import fcntl >>> a = open('/tmp/locktest', 'w') >>> b = open('/tmp/locktest', 'w') >>> fcntl.lockf(a,…
Jatin Kumar
  • 2,635
  • 9
  • 36
  • 46
18
votes
2 answers

Release of flock in case of errors?

Imagine the following Perl code (here in pseudo code): successfully acquired flock for FILEHANDLER # line 1 some error or maybe simply a call to exit() # line 2 close FILEHANDLER (which also releases the lock) # line 3 In this case I…
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
17
votes
2 answers

bash flock: exit if can't acquire lock

The following lock mechanism is used for preventing a cron job from running concurrently: #!/bin/bash echo "Before critical section" ( flock -e 200 echo "In critical section" sleep 5 ) 200>/tmp/blah.lockfile echo "After critical…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
17
votes
1 answer

Is there a portable way to put a timeout on flock()?

flock() is PHP's portable advisory file locking function. They explicitly promote that it even works under windows: flock() allows you to perform a simple reader/writer model which can be used on virtually every platform (including most Unix…
Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67
17
votes
1 answer

PHP check if file locked with flock()?

Will fopen() fail if a file exists, but is currently locked with LOCK_EX? Or do I have to open it, and then try and set a lock, in order to determine if one already exists? I've also read that flock() will; pause [the script] untill you get the…
TheDeadMedic
  • 9,948
  • 2
  • 35
  • 50
15
votes
11 answers

Using AppleScript to grab the URL from the frontmost window in web browsers: The definitive list

I built a [widget][1] that grabs the URL from the frontmost window in Safari, then allows you to shorten it using the tr.im API. Works sweet as. I want to make this more flexible, so am investigating how to grab an URL from other browsers. Here's…
Andrew Hedges
  • 21,688
  • 16
  • 67
  • 79
14
votes
7 answers

PHP flock() alternative

PHP's documentation page for flock() indicates that it's not safe to use under IIS. If I can't rely on flock under all circumstances, is there another way I could safely achieve the same thing?
Matty
  • 33,203
  • 13
  • 65
  • 93
14
votes
1 answer

Invisible files associated with OS X keychains

It seems that a keychain file (with extension .keychain) will usually have an invisible file associated with it, located in the same directory. This invisible file always has these properties: It is empty (zero bytes). Its permissions are 0444…
TachyonVortex
  • 8,242
  • 3
  • 48
  • 63
1
2 3
19 20