getting scripts to run exclusively












2















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...)










share|improve this question























  • 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


















2















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...)










share|improve this question























  • 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
















2












2








2


0






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...)










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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





















  • 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














2 Answers
2






active

oldest

votes


















0














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=""





share|improve this answer
























  • 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



















0














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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=""





    share|improve this answer
























    • 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
















    0














    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=""





    share|improve this answer
























    • 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














    0












    0








    0







    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=""





    share|improve this answer













    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=""






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 18 at 20:01









        HardcoreHenryHardcoreHenry

        1,291419




        1,291419






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Callistus III

            Ostreoida

            Plistias Cous