3 ms·
A fairly common pattern would be trying to create a specifically named file with the O_CREAT and O_EXCL flags. That's atomic, so you don't need flock. If it f
by tyingq 5y ago
A fairly common pattern would be trying to create a specifically named file with the O_CREAT and O_EXCL flags. That's atomic, so you don't need flock. If it fails, then exit. This pattern is also often extended where you write your pid to the file and subsequent runs do a sanity check that your pid is actually running, and unlink the file if not.
That said systemd-timers, as mentioned in many other comments, is probably the right answer here.
- nickcw 5y agoflock locks are released when the file is no longer open. This means that no matter how your command exits the OS will close open files and release the lock for you. The problem with creating files is that you then have the stale lock file problem to deal with which adds a lot more complication.
- the2ndfloorguy 5y agoIs there any other way without using lock files? Using socket or something?
- tyingq 5y agoI suppose you could try listening on a socket at a specific port. The next process would get EADDRINUSE if you were still running. There's also sysv or posix semaphores.