getting scripts to run exclusively
I'm trying to get a bash script to run exclusively -- if another instance of the script is already running, then wait for the other instance to finish before starting the new one. I found some references to flock which sounds like it should do what I need, but it doesn't seem to be working the way I expect. I have the following script:
#!/bin/bash
inst=$1
lock=/nobackup/julvr/locks/_tst.lk
exec 200>$lock
flock -x -w30 200 || { echo "$inst: failed flock" && exit 1; }
echo "$inst:got lock"
for i in {1..2}; do
echo "$inst: $i"
sleep 1
done
echo "$inst:done script";
And then I run
> flocktest.sh test1 & flocktest.sh test2
[1] 25213
test1:got lock
test1: 1
test2:got lock
test2: 1
test1: 2
test2: 2
test1:done script
test2:done script
[1]+ Done flocktest.sh test1
It seems both instances of flocktest are running in parallel... When does flock release its lock? How to make it keep the lock until the script is complete?
(An aside, if I do flock -x -w 20 200, then it complains flock: 20: fcntl: Bad file descriptor..., which seems odd, as the man page seems to imply I can add a --w timeout parameter before the lockfile...)
bash flock
add a comment |
I'm trying to get a bash script to run exclusively -- if another instance of the script is already running, then wait for the other instance to finish before starting the new one. I found some references to flock which sounds like it should do what I need, but it doesn't seem to be working the way I expect. I have the following script:
#!/bin/bash
inst=$1
lock=/nobackup/julvr/locks/_tst.lk
exec 200>$lock
flock -x -w30 200 || { echo "$inst: failed flock" && exit 1; }
echo "$inst:got lock"
for i in {1..2}; do
echo "$inst: $i"
sleep 1
done
echo "$inst:done script";
And then I run
> flocktest.sh test1 & flocktest.sh test2
[1] 25213
test1:got lock
test1: 1
test2:got lock
test2: 1
test1: 2
test2: 2
test1:done script
test2:done script
[1]+ Done flocktest.sh test1
It seems both instances of flocktest are running in parallel... When does flock release its lock? How to make it keep the lock until the script is complete?
(An aside, if I do flock -x -w 20 200, then it complains flock: 20: fcntl: Bad file descriptor..., which seems odd, as the man page seems to imply I can add a --w timeout parameter before the lockfile...)
bash flock
Works for me. Which OS are you running this on, and what fs is your lock file on?
– that other guy
Jan 18 at 18:05
The file is on a Linux box (2.6.32-696.18.7.el6.x86_64), and the file is on a mounted directory.
– HardcoreHenry
Jan 18 at 18:29
Ok, got it. Apperently someone remapped the PATH, and it was grabbing a really old version of flock, which doesn't work the way I'd expect...
– HardcoreHenry
Jan 18 at 18:31
add a comment |
I'm trying to get a bash script to run exclusively -- if another instance of the script is already running, then wait for the other instance to finish before starting the new one. I found some references to flock which sounds like it should do what I need, but it doesn't seem to be working the way I expect. I have the following script:
#!/bin/bash
inst=$1
lock=/nobackup/julvr/locks/_tst.lk
exec 200>$lock
flock -x -w30 200 || { echo "$inst: failed flock" && exit 1; }
echo "$inst:got lock"
for i in {1..2}; do
echo "$inst: $i"
sleep 1
done
echo "$inst:done script";
And then I run
> flocktest.sh test1 & flocktest.sh test2
[1] 25213
test1:got lock
test1: 1
test2:got lock
test2: 1
test1: 2
test2: 2
test1:done script
test2:done script
[1]+ Done flocktest.sh test1
It seems both instances of flocktest are running in parallel... When does flock release its lock? How to make it keep the lock until the script is complete?
(An aside, if I do flock -x -w 20 200, then it complains flock: 20: fcntl: Bad file descriptor..., which seems odd, as the man page seems to imply I can add a --w timeout parameter before the lockfile...)
bash flock
I'm trying to get a bash script to run exclusively -- if another instance of the script is already running, then wait for the other instance to finish before starting the new one. I found some references to flock which sounds like it should do what I need, but it doesn't seem to be working the way I expect. I have the following script:
#!/bin/bash
inst=$1
lock=/nobackup/julvr/locks/_tst.lk
exec 200>$lock
flock -x -w30 200 || { echo "$inst: failed flock" && exit 1; }
echo "$inst:got lock"
for i in {1..2}; do
echo "$inst: $i"
sleep 1
done
echo "$inst:done script";
And then I run
> flocktest.sh test1 & flocktest.sh test2
[1] 25213
test1:got lock
test1: 1
test2:got lock
test2: 1
test1: 2
test2: 2
test1:done script
test2:done script
[1]+ Done flocktest.sh test1
It seems both instances of flocktest are running in parallel... When does flock release its lock? How to make it keep the lock until the script is complete?
(An aside, if I do flock -x -w 20 200, then it complains flock: 20: fcntl: Bad file descriptor..., which seems odd, as the man page seems to imply I can add a --w timeout parameter before the lockfile...)
bash flock
bash flock
asked Jan 18 at 17:24
HardcoreHenryHardcoreHenry
1,291419
1,291419
Works for me. Which OS are you running this on, and what fs is your lock file on?
– that other guy
Jan 18 at 18:05
The file is on a Linux box (2.6.32-696.18.7.el6.x86_64), and the file is on a mounted directory.
– HardcoreHenry
Jan 18 at 18:29
Ok, got it. Apperently someone remapped the PATH, and it was grabbing a really old version of flock, which doesn't work the way I'd expect...
– HardcoreHenry
Jan 18 at 18:31
add a comment |
Works for me. Which OS are you running this on, and what fs is your lock file on?
– that other guy
Jan 18 at 18:05
The file is on a Linux box (2.6.32-696.18.7.el6.x86_64), and the file is on a mounted directory.
– HardcoreHenry
Jan 18 at 18:29
Ok, got it. Apperently someone remapped the PATH, and it was grabbing a really old version of flock, which doesn't work the way I'd expect...
– HardcoreHenry
Jan 18 at 18:31
Works for me. Which OS are you running this on, and what fs is your lock file on?
– that other guy
Jan 18 at 18:05
Works for me. Which OS are you running this on, and what fs is your lock file on?
– that other guy
Jan 18 at 18:05
The file is on a Linux box (2.6.32-696.18.7.el6.x86_64), and the file is on a mounted directory.
– HardcoreHenry
Jan 18 at 18:29
The file is on a Linux box (2.6.32-696.18.7.el6.x86_64), and the file is on a mounted directory.
– HardcoreHenry
Jan 18 at 18:29
Ok, got it. Apperently someone remapped the PATH, and it was grabbing a really old version of flock, which doesn't work the way I'd expect...
– HardcoreHenry
Jan 18 at 18:31
Ok, got it. Apperently someone remapped the PATH, and it was grabbing a really old version of flock, which doesn't work the way I'd expect...
– HardcoreHenry
Jan 18 at 18:31
add a comment |
2 Answers
2
active
oldest
votes
flock seems to me very complicated.
Perhaps you can try this way.
cat script_unique.sh
while test -n "$run_sh"
do
sleep 2
done
export run_sh="run_sh"
sleep 2
echo "$run_sh"
sleep 4
echo "$0 $1"
run_sh=""
Thanks, but flock is indeed cleaner. My issue was that I was using an older version of flock, with a different interface than what the man page described. I'm closing this issue.
– HardcoreHenry
Jan 18 at 19:46
add a comment |
Ok, I found my bug -- I was using a really old version of flock which had a different interface than the one described in the man page. I updated to a new version of flock, and it worked.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258750%2fgetting-scripts-to-run-exclusively%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
flock seems to me very complicated.
Perhaps you can try this way.
cat script_unique.sh
while test -n "$run_sh"
do
sleep 2
done
export run_sh="run_sh"
sleep 2
echo "$run_sh"
sleep 4
echo "$0 $1"
run_sh=""
Thanks, but flock is indeed cleaner. My issue was that I was using an older version of flock, with a different interface than what the man page described. I'm closing this issue.
– HardcoreHenry
Jan 18 at 19:46
add a comment |
flock seems to me very complicated.
Perhaps you can try this way.
cat script_unique.sh
while test -n "$run_sh"
do
sleep 2
done
export run_sh="run_sh"
sleep 2
echo "$run_sh"
sleep 4
echo "$0 $1"
run_sh=""
Thanks, but flock is indeed cleaner. My issue was that I was using an older version of flock, with a different interface than what the man page described. I'm closing this issue.
– HardcoreHenry
Jan 18 at 19:46
add a comment |
flock seems to me very complicated.
Perhaps you can try this way.
cat script_unique.sh
while test -n "$run_sh"
do
sleep 2
done
export run_sh="run_sh"
sleep 2
echo "$run_sh"
sleep 4
echo "$0 $1"
run_sh=""
flock seems to me very complicated.
Perhaps you can try this way.
cat script_unique.sh
while test -n "$run_sh"
do
sleep 2
done
export run_sh="run_sh"
sleep 2
echo "$run_sh"
sleep 4
echo "$0 $1"
run_sh=""
answered Jan 18 at 18:57
ctac_ctac_
1,807138
1,807138
Thanks, but flock is indeed cleaner. My issue was that I was using an older version of flock, with a different interface than what the man page described. I'm closing this issue.
– HardcoreHenry
Jan 18 at 19:46
add a comment |
Thanks, but flock is indeed cleaner. My issue was that I was using an older version of flock, with a different interface than what the man page described. I'm closing this issue.
– HardcoreHenry
Jan 18 at 19:46
Thanks, but flock is indeed cleaner. My issue was that I was using an older version of flock, with a different interface than what the man page described. I'm closing this issue.
– HardcoreHenry
Jan 18 at 19:46
Thanks, but flock is indeed cleaner. My issue was that I was using an older version of flock, with a different interface than what the man page described. I'm closing this issue.
– HardcoreHenry
Jan 18 at 19:46
add a comment |
Ok, I found my bug -- I was using a really old version of flock which had a different interface than the one described in the man page. I updated to a new version of flock, and it worked.
add a comment |
Ok, I found my bug -- I was using a really old version of flock which had a different interface than the one described in the man page. I updated to a new version of flock, and it worked.
add a comment |
Ok, I found my bug -- I was using a really old version of flock which had a different interface than the one described in the man page. I updated to a new version of flock, and it worked.
Ok, I found my bug -- I was using a really old version of flock which had a different interface than the one described in the man page. I updated to a new version of flock, and it worked.
answered Jan 18 at 20:01
HardcoreHenryHardcoreHenry
1,291419
1,291419
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258750%2fgetting-scripts-to-run-exclusively%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Works for me. Which OS are you running this on, and what fs is your lock file on?
– that other guy
Jan 18 at 18:05
The file is on a Linux box (2.6.32-696.18.7.el6.x86_64), and the file is on a mounted directory.
– HardcoreHenry
Jan 18 at 18:29
Ok, got it. Apperently someone remapped the PATH, and it was grabbing a really old version of flock, which doesn't work the way I'd expect...
– HardcoreHenry
Jan 18 at 18:31