Bash if command doesn't finish in X time?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












I'm trying to run something like:



sudo dhclient $wifi || otherFunction


Problem is when dhclient fails it just hangs instead of throwing an error.



How can I re-write the above so dhclient is killed and otherFunction gets called if dhclient doesn't finish in 60 seconds?







share|improve this question
























    up vote
    4
    down vote

    favorite
    1












    I'm trying to run something like:



    sudo dhclient $wifi || otherFunction


    Problem is when dhclient fails it just hangs instead of throwing an error.



    How can I re-write the above so dhclient is killed and otherFunction gets called if dhclient doesn't finish in 60 seconds?







    share|improve this question






















      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      I'm trying to run something like:



      sudo dhclient $wifi || otherFunction


      Problem is when dhclient fails it just hangs instead of throwing an error.



      How can I re-write the above so dhclient is killed and otherFunction gets called if dhclient doesn't finish in 60 seconds?







      share|improve this question












      I'm trying to run something like:



      sudo dhclient $wifi || otherFunction


      Problem is when dhclient fails it just hangs instead of throwing an error.



      How can I re-write the above so dhclient is killed and otherFunction gets called if dhclient doesn't finish in 60 seconds?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '17 at 20:18









      Philip Kirkbride

      2,2922470




      2,2922470




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          Your timeout tag gives it all away:



          sudo timeout 60 dhclient $wifi || otherFunction


          An example:



          sudo timeout 3 sleep 5 || echo finished early


          This uses the timeout utility provided by the GNU coreutils package on Linux.






          share|improve this answer




















          • doesn't work on MacOS unfortunately
            – Alexander Mills
            Apr 20 at 22:13

















          up vote
          2
          down vote













          Use timeout.



          timeout 2 sleep 1
          echo $?
          0

          timeout 1 sleep 2
          echo $?
          124





          share|improve this answer




















          • doesn't work on MacOS unfortunately
            – Alexander Mills
            Apr 20 at 22:13

















          up vote
          1
          down vote













          Use the timeout packed as gtimeout with the coreutils port in brew:



          brew install coreutils
          gtimeout --help


          That'll work with /usr/local/bin in your PATH. If you want to use timeout as originally named, add /usr/local/opt/coreutils/libexec/gnubin to your PATH.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405337%2fbash-if-command-doesnt-finish-in-x-time%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            6
            down vote



            accepted










            Your timeout tag gives it all away:



            sudo timeout 60 dhclient $wifi || otherFunction


            An example:



            sudo timeout 3 sleep 5 || echo finished early


            This uses the timeout utility provided by the GNU coreutils package on Linux.






            share|improve this answer




















            • doesn't work on MacOS unfortunately
              – Alexander Mills
              Apr 20 at 22:13














            up vote
            6
            down vote



            accepted










            Your timeout tag gives it all away:



            sudo timeout 60 dhclient $wifi || otherFunction


            An example:



            sudo timeout 3 sleep 5 || echo finished early


            This uses the timeout utility provided by the GNU coreutils package on Linux.






            share|improve this answer




















            • doesn't work on MacOS unfortunately
              – Alexander Mills
              Apr 20 at 22:13












            up vote
            6
            down vote



            accepted







            up vote
            6
            down vote



            accepted






            Your timeout tag gives it all away:



            sudo timeout 60 dhclient $wifi || otherFunction


            An example:



            sudo timeout 3 sleep 5 || echo finished early


            This uses the timeout utility provided by the GNU coreutils package on Linux.






            share|improve this answer












            Your timeout tag gives it all away:



            sudo timeout 60 dhclient $wifi || otherFunction


            An example:



            sudo timeout 3 sleep 5 || echo finished early


            This uses the timeout utility provided by the GNU coreutils package on Linux.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 '17 at 20:21









            Jeff Schaller

            32.1k849109




            32.1k849109











            • doesn't work on MacOS unfortunately
              – Alexander Mills
              Apr 20 at 22:13
















            • doesn't work on MacOS unfortunately
              – Alexander Mills
              Apr 20 at 22:13















            doesn't work on MacOS unfortunately
            – Alexander Mills
            Apr 20 at 22:13




            doesn't work on MacOS unfortunately
            – Alexander Mills
            Apr 20 at 22:13












            up vote
            2
            down vote













            Use timeout.



            timeout 2 sleep 1
            echo $?
            0

            timeout 1 sleep 2
            echo $?
            124





            share|improve this answer




















            • doesn't work on MacOS unfortunately
              – Alexander Mills
              Apr 20 at 22:13














            up vote
            2
            down vote













            Use timeout.



            timeout 2 sleep 1
            echo $?
            0

            timeout 1 sleep 2
            echo $?
            124





            share|improve this answer




















            • doesn't work on MacOS unfortunately
              – Alexander Mills
              Apr 20 at 22:13












            up vote
            2
            down vote










            up vote
            2
            down vote









            Use timeout.



            timeout 2 sleep 1
            echo $?
            0

            timeout 1 sleep 2
            echo $?
            124





            share|improve this answer












            Use timeout.



            timeout 2 sleep 1
            echo $?
            0

            timeout 1 sleep 2
            echo $?
            124






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 '17 at 20:22









            Matt Clark

            204211




            204211











            • doesn't work on MacOS unfortunately
              – Alexander Mills
              Apr 20 at 22:13
















            • doesn't work on MacOS unfortunately
              – Alexander Mills
              Apr 20 at 22:13















            doesn't work on MacOS unfortunately
            – Alexander Mills
            Apr 20 at 22:13




            doesn't work on MacOS unfortunately
            – Alexander Mills
            Apr 20 at 22:13










            up vote
            1
            down vote













            Use the timeout packed as gtimeout with the coreutils port in brew:



            brew install coreutils
            gtimeout --help


            That'll work with /usr/local/bin in your PATH. If you want to use timeout as originally named, add /usr/local/opt/coreutils/libexec/gnubin to your PATH.






            share|improve this answer
























              up vote
              1
              down vote













              Use the timeout packed as gtimeout with the coreutils port in brew:



              brew install coreutils
              gtimeout --help


              That'll work with /usr/local/bin in your PATH. If you want to use timeout as originally named, add /usr/local/opt/coreutils/libexec/gnubin to your PATH.






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                Use the timeout packed as gtimeout with the coreutils port in brew:



                brew install coreutils
                gtimeout --help


                That'll work with /usr/local/bin in your PATH. If you want to use timeout as originally named, add /usr/local/opt/coreutils/libexec/gnubin to your PATH.






                share|improve this answer












                Use the timeout packed as gtimeout with the coreutils port in brew:



                brew install coreutils
                gtimeout --help


                That'll work with /usr/local/bin in your PATH. If you want to use timeout as originally named, add /usr/local/opt/coreutils/libexec/gnubin to your PATH.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 16 at 1:21









                Garth Kidd

                1113




                1113



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405337%2fbash-if-command-doesnt-finish-in-x-time%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?