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
0
votes
2 answers

Using Flock in Bash so Request is Made Only Once

I'm trying to configure my script in such a way that If some data isn't available, try to fetch it If another process is already fetching it, wait for that process to finish Use the data And from here I found this very nice example of flock: exec…
Jordan Mackie
  • 2,264
  • 4
  • 25
  • 45
0
votes
1 answer

pkill isn't fully killing process?

I'm doing some testing with flock and pkill for a test.sh script that I'm calling from cron and I ran into something I don't understand. The test.sh is scheduled as a * * * * * job in cron. Its a very simple script that for testing purposes writes a…
sjaak
  • 644
  • 1
  • 7
  • 18
0
votes
0 answers

PHP - does file_exist return false if checking a locked file

I have a script that runs with each visitor. Each visit writes some information to a file. I first check if the file exists using file_exists, and it if does I write to it using file_put_contents using the LOCK_EX flag. For example:- $file =…
0
votes
1 answer

ruby file locking error Errno::EBADF in solaris

Am trying to lock one executable script to make sure it doesn't run second time when there is another process running. Here is my code, if $0 == __FILE__ if File.new(__FILE__).flock(File::LOCK_EX | File::LOCK_NB) main() end end and getting…
Karthi1234
  • 949
  • 1
  • 8
  • 28
0
votes
1 answer

Weird behaviour with crontab an flock

I'm trying to run only one instance of a program, this is my runner script which is called from crontab: #!/bin/bash exec 9>./lockfile if ! flock -n 9 ; then echo "another instance of $0 is running"; exit 1 fi node $(dirname…
Francisco Rangel
  • 129
  • 1
  • 11
0
votes
2 answers

C++ Ubuntu not releasing lock on lock file when terminated

I have a C++ script, which checks whether any action has to be done and if so it starts the right processor C++ script. However, since it runs every x minutes it also checks whether the processor isn't still running using lock files. I use the…
TVA van Hesteren
  • 1,031
  • 3
  • 20
  • 47
0
votes
0 answers

How does python flock() work?

I have the following code: import os, fcntl f = open('./testfile.t', 'wb') f.write(os.urandom(64)) fcntl.flock(f.fileno(), fcntl.LOCK_EX) print 'Locked' try: f2 = open('./testfile.t', 'wb') except IOError as err: print err.errno …
sotona
  • 1,731
  • 2
  • 24
  • 34
0
votes
1 answer

Offset Crons with Sleep and Flock together

I run several cron jobs every minute - I use flock to prevent overlapping as a couple of the scripts may run for over minute: * * * * * flock -n /path/to/lock-process-1.txt php /path/to/process-1.php * * * * * flock -n /path/to/lock-process-2.txt…
Ryan
  • 152
  • 1
  • 13
0
votes
1 answer

PHP Lock file while reading - flock() makes my file blank

As you can see in the code below, I'm trying to use flock to prevent other clients to acess the php (actually multiple users will acess this something like 10 times per second, each one), as I've found searching here... But this is not working. My…
0
votes
0 answers

Shell: How do I make sure only one command duplicate will be executed after the current one

I am watching my project dir for the file change and running sync script whenever files change. Certainly I do not want to run the second synchronization before the first one is done. flock utility seems to be fit for preventing second sync from…
Artem
  • 776
  • 5
  • 18
0
votes
1 answer

Using flock to lock file for X time

I am a Linux novice and currently developing a security system using Raspberry Pi 3 and MotionEye. To get notifications via e-mail, I am attempting to create a custom shell script that will send an e-mail if there is motion, lock for X minutes, then…
user8038070
0
votes
0 answers

Is flock(fd, LOCK_UN) undefined when the lock is already unlocked?

The man pages ( https://linux.die.net/man/2/flock ) are not clear regarding whether a LOCK_UN operation on flock() is allowed if unlock has already been called in another thread. In my case, multiple threads could be reading the same file via…
synchronizer
  • 1,955
  • 1
  • 14
  • 37
0
votes
1 answer

Kill All Processes Spawned by NPM During Shell Script

I have a shell script that looks roughly like the following: #!/bin/bash # Script variables NPM="/usr/bin/npm" # Start several sub-processes in a loop in parallel for i in {1..4}; do $NPM run -s long_running_script >>…
Oscar Wahltinez
  • 1,155
  • 3
  • 12
  • 24
0
votes
0 answers

How to implement lock mechanism in FAT system?

I have read the PHP manual and understand that flock may not work in FAT environment.
Ting Ping
  • 1,145
  • 7
  • 18
  • 34
0
votes
1 answer

php script - Redirect input to another instance

I have a php script who use flock() to deny multiple instance if script already running. I would like the arguments provided in the script call are transferred to the existing process who would be able to handle them. ie : test.php : #!/usr/bin/env…
Khorwin
  • 445
  • 1
  • 9
  • 27