Is there lurking somewhere in bash or as a utility program something I can use on a command line to construct a semaphore? The idea is that I have some resource and a couple of processes that want that resource. I want to make sure that if one gets the resource, the other is locked out until the first one is finished. For what it's worth, here's the specific case I'm interested in. I want to back up my home directory once a day. Easy enough, it seems -- create a cron job that does the deed at noon each day. But what if my machine isn't turned on at noon (since I don't leave it on all the time)? Again -- easy enough: do the backup when the machine is turned on, and set a signal indicating that it's been done. If the signal is set at noon, then don't do the backup. But things get interesting if I turn the machine on very slightly before noon. Then it's possible that the job will either get done twice or not at all if I don't handle the signal correctly. That's the problem that semaphores are supposed to solve. Paul
On Tue, 4 Jan 2005 22:48:22 -0500, Paul W. Abrahams wrote:
Is there lurking somewhere in bash or as a utility program something I can use on a command line to construct a semaphore? The idea is that I have some resource and a couple of processes that want that resource. I want to make sure that if one gets the resource, the other is locked out until the first one is finished.
For what it's worth, here's the specific case I'm interested in. I want to back up my home directory once a day. Easy enough, it seems -- create a cron job that does the deed at noon each day. But what if my machine isn't turned on at noon (since I don't leave it on all the time)? Again -- easy enough: do the backup when the machine is turned on, and set a signal indicating that it's been done. If the signal is set at noon, then don't do the backup.
But things get interesting if I turn the machine on very slightly before noon. Then it's possible that the job will either get done twice or not at all if I don't handle the signal correctly. That's the problem that semaphores are supposed to solve.
Paul
I don't know of one, but you can write your own in c code pretty easily.
#include
On Tue, 4 Jan 2005 22:48:22 -0500, Paul W. Abrahams
Is there lurking somewhere in bash or as a utility program something I can use on a command line to construct a semaphore? The idea is that I have some resource and a couple of processes that want that resource. I want to make sure that if one gets the resource, the other is locked out until the first one is finished.
For what it's worth, here's the specific case I'm interested in. I want to back up my home directory once a day. Easy enough, it seems -- create a cron job that does the deed at noon each day. But what if my machine isn't turned on at noon (since I don't leave it on all the time)? Again -- easy enough: do the backup when the machine is turned on, and set a signal indicating that it's been done. If the signal is set at noon, then don't do the backup.
But things get interesting if I turn the machine on very slightly before noon. Then it's possible that the job will either get done twice or not at all if I don't handle the signal correctly. That's the problem that semaphores are supposed to solve.
Paul
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
Hi Paul, This is not really perfect, as if the 2 processes run almost in the same time, you'll have problems. But, otherwise I would check for the existence of a temp "lock" file in the beginning of the script, and if it does not exist, I'll create it and run the job, otherwise I'll skip. And at the end of script I'll delete the file. As I said, this is not perfect, but for such a problem as yours it should be OK. Cheers Sunny -- Get Firefox http://www.spreadfirefox.com/?q=affiliates&id=10745&t=85
On Wednesday 05 January 2005 19:45, Sunny wrote:
On Tue, 4 Jan 2005 22:48:22 -0500, Paul W. Abrahams
wrote: Is there lurking somewhere in bash or as a utility program something I can use on a command line to construct a semaphore? The idea is that I have some resource and a couple of processes that want that resource. I want to make sure that if one gets the resource, the other is locked out until the first one is finished.
Type man lockfile to find all you need... Paul -- Paul Hewlett (Linux #359543) Email:`echo az.oc.evitcaten@ttelweh | rev` Tel: +27 21 852 8812 Cel : +27 72 719 2725 FAX: +27 866720563 --
On Wednesday 05 January 2005 3:54 pm, Paul Hewlett wrote:
On Wednesday 05 January 2005 19:45, Sunny wrote:
On Tue, 4 Jan 2005 22:48:22 -0500, Paul W. Abrahams
wrote: Is there lurking somewhere in bash or as a utility program something I can use on a command line to construct a semaphore? The idea is that I have some resource and a couple of processes that want that resource. I want to make sure that if one gets the resource, the other is locked out until the first one is finished.
Type
man lockfile
Just what the doctor ordered -- thanks! I wonder how I might have discovered that gem myself. I tried googling "linux semaphore" and came up empty-handed. Paul
Þann Miðvikudagur 05 janúar 2005 22:59 skrifaði Paul W. Abrahams:
man lockfile
Just what the doctor ordered -- thanks!
I wonder how I might have discovered that gem myself. I tried googling "linux semaphore" and came up empty-handed.
apropos semaphore
Paul
* Paul W. Abrahams
I wonder how I might have discovered that gem myself. I tried googling "linux semaphore" and came up empty-handed.
man -k semaphore -- Patrick Shanahan Registered Linux User #207535 http://wahoo.no-ip.org @ http://counter.li.org HOG # US1244711 Photo Album: http://wahoo.no-ip.org/photos
Paul, On Tuesday 04 January 2005 19:48, Paul W. Abrahams wrote:
Is there lurking somewhere in bash or as a utility program something I can use on a command line to construct a semaphore?
I don't know about lurking, though "apropos lock" produces 234 hits on my system. Narrowing it down to commands (i.e., excluding library functions) by filtering the apropos output through "grep '(1'" cuts that down to a mere 30, which makes it easy to pick out "lockfile": % man lockfile LOCKFILE(1) LOCKFILE(1) NAME lockfile - conditional semaphore-file creator SYNOPSIS lockfile -sleeptime | -r retries | -l locktimeout | -s suspend | -! | -ml | -mu | file name ... DESCRIPTION lockfile can be used to create one or more semaphore files. If lockfile can't create all the specified files (in the specified order), it waits sleeptime (defaults to 8) seconds and retries the last file that didn't succeed. You can specify the number of retries to do until failure is returned. If the number of retries is -1 (default, i.e., -r-1) lockfile will retry forever. ... I believe this will do what you want.
...
Paul
And by the way, I did not know of this command before you asked. Randall Schulz
participants (7)
-
Greg Freemyer
-
Patrick Shanahan
-
Paul Hewlett
-
Paul W. Abrahams
-
Randall R Schulz
-
Sunny
-
Örn Einar Hansen