Construct a while loop around a command throwing a segmentation fault

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











up vote
0
down vote

favorite












I have a program that throws a segmentation fault on certain circumstances. I want to execute a command when the segmentation fault occurs to process the data, then execute the command again, and keep doing so until the segmentation fault stops.



As a rough attempt at pseudo code,



dodgy_command

while SegFault
dataProcessing
dodgy_command
end


I think I need to be using a Trap command, but I don't understand the syntax for this command.










share|improve this question



























    up vote
    0
    down vote

    favorite












    I have a program that throws a segmentation fault on certain circumstances. I want to execute a command when the segmentation fault occurs to process the data, then execute the command again, and keep doing so until the segmentation fault stops.



    As a rough attempt at pseudo code,



    dodgy_command

    while SegFault
    dataProcessing
    dodgy_command
    end


    I think I need to be using a Trap command, but I don't understand the syntax for this command.










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a program that throws a segmentation fault on certain circumstances. I want to execute a command when the segmentation fault occurs to process the data, then execute the command again, and keep doing so until the segmentation fault stops.



      As a rough attempt at pseudo code,



      dodgy_command

      while SegFault
      dataProcessing
      dodgy_command
      end


      I think I need to be using a Trap command, but I don't understand the syntax for this command.










      share|improve this question















      I have a program that throws a segmentation fault on certain circumstances. I want to execute a command when the segmentation fault occurs to process the data, then execute the command again, and keep doing so until the segmentation fault stops.



      As a rough attempt at pseudo code,



      dodgy_command

      while SegFault
      dataProcessing
      dodgy_command
      end


      I think I need to be using a Trap command, but I don't understand the syntax for this command.







      segmentation-fault






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 5 at 17:08









      Rui F Ribeiro

      38.6k1479128




      38.6k1479128










      asked Dec 5 at 15:53









      Pablo

      1033




      1033




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          When a program aborts abnormally the exit code (as seen by the shell) typically has the high bit set so the value is 128 or higher. So a simple solution might be



          dodgy_command

          while [ $? -ge 128 ]
          do
          process_data
          dodgy_command
          done


          If you specifically only want a segfault and not any other type of error, the while line becomes $? -eq 139 (because SEGV is signal 11; 128+11=139).



          If you don't get an high valued exit code on failure then it probably means the application is trapping the error, itself, and forcing a different exit code.






          share|improve this answer



























            up vote
            0
            down vote













            You don't mention what language you need to do this in. So, I'm assuming shell script.



            You are correct, you want to use a trap. The trap in bash takes a string to execute when a signal is received. I tend to make it a function call so I can clearly define what I want done when I trap the signal.



            Here is a sample script.



            #!/bin/bash

            # define the function that will handle the trap
            sigSegFault() echo "Inside sigSegFault";

            # Trap the Segmentation Violation (signal 11) signal
            trap sigSegFault SEGV

            # Start infinite loop
            while [[ true ]]; do
            sleep 10
            done

            exit 0


            Simplistic, but contains the basics of what you are asking for.






            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: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2funix.stackexchange.com%2fquestions%2f486180%2fconstruct-a-while-loop-around-a-command-throwing-a-segmentation-fault%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








              up vote
              5
              down vote



              accepted










              When a program aborts abnormally the exit code (as seen by the shell) typically has the high bit set so the value is 128 or higher. So a simple solution might be



              dodgy_command

              while [ $? -ge 128 ]
              do
              process_data
              dodgy_command
              done


              If you specifically only want a segfault and not any other type of error, the while line becomes $? -eq 139 (because SEGV is signal 11; 128+11=139).



              If you don't get an high valued exit code on failure then it probably means the application is trapping the error, itself, and forcing a different exit code.






              share|improve this answer
























                up vote
                5
                down vote



                accepted










                When a program aborts abnormally the exit code (as seen by the shell) typically has the high bit set so the value is 128 or higher. So a simple solution might be



                dodgy_command

                while [ $? -ge 128 ]
                do
                process_data
                dodgy_command
                done


                If you specifically only want a segfault and not any other type of error, the while line becomes $? -eq 139 (because SEGV is signal 11; 128+11=139).



                If you don't get an high valued exit code on failure then it probably means the application is trapping the error, itself, and forcing a different exit code.






                share|improve this answer






















                  up vote
                  5
                  down vote



                  accepted







                  up vote
                  5
                  down vote



                  accepted






                  When a program aborts abnormally the exit code (as seen by the shell) typically has the high bit set so the value is 128 or higher. So a simple solution might be



                  dodgy_command

                  while [ $? -ge 128 ]
                  do
                  process_data
                  dodgy_command
                  done


                  If you specifically only want a segfault and not any other type of error, the while line becomes $? -eq 139 (because SEGV is signal 11; 128+11=139).



                  If you don't get an high valued exit code on failure then it probably means the application is trapping the error, itself, and forcing a different exit code.






                  share|improve this answer












                  When a program aborts abnormally the exit code (as seen by the shell) typically has the high bit set so the value is 128 or higher. So a simple solution might be



                  dodgy_command

                  while [ $? -ge 128 ]
                  do
                  process_data
                  dodgy_command
                  done


                  If you specifically only want a segfault and not any other type of error, the while line becomes $? -eq 139 (because SEGV is signal 11; 128+11=139).



                  If you don't get an high valued exit code on failure then it probably means the application is trapping the error, itself, and forcing a different exit code.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 5 at 16:17









                  Stephen Harris

                  24.1k24477




                  24.1k24477






















                      up vote
                      0
                      down vote













                      You don't mention what language you need to do this in. So, I'm assuming shell script.



                      You are correct, you want to use a trap. The trap in bash takes a string to execute when a signal is received. I tend to make it a function call so I can clearly define what I want done when I trap the signal.



                      Here is a sample script.



                      #!/bin/bash

                      # define the function that will handle the trap
                      sigSegFault() echo "Inside sigSegFault";

                      # Trap the Segmentation Violation (signal 11) signal
                      trap sigSegFault SEGV

                      # Start infinite loop
                      while [[ true ]]; do
                      sleep 10
                      done

                      exit 0


                      Simplistic, but contains the basics of what you are asking for.






                      share|improve this answer


























                        up vote
                        0
                        down vote













                        You don't mention what language you need to do this in. So, I'm assuming shell script.



                        You are correct, you want to use a trap. The trap in bash takes a string to execute when a signal is received. I tend to make it a function call so I can clearly define what I want done when I trap the signal.



                        Here is a sample script.



                        #!/bin/bash

                        # define the function that will handle the trap
                        sigSegFault() echo "Inside sigSegFault";

                        # Trap the Segmentation Violation (signal 11) signal
                        trap sigSegFault SEGV

                        # Start infinite loop
                        while [[ true ]]; do
                        sleep 10
                        done

                        exit 0


                        Simplistic, but contains the basics of what you are asking for.






                        share|improve this answer
























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You don't mention what language you need to do this in. So, I'm assuming shell script.



                          You are correct, you want to use a trap. The trap in bash takes a string to execute when a signal is received. I tend to make it a function call so I can clearly define what I want done when I trap the signal.



                          Here is a sample script.



                          #!/bin/bash

                          # define the function that will handle the trap
                          sigSegFault() echo "Inside sigSegFault";

                          # Trap the Segmentation Violation (signal 11) signal
                          trap sigSegFault SEGV

                          # Start infinite loop
                          while [[ true ]]; do
                          sleep 10
                          done

                          exit 0


                          Simplistic, but contains the basics of what you are asking for.






                          share|improve this answer














                          You don't mention what language you need to do this in. So, I'm assuming shell script.



                          You are correct, you want to use a trap. The trap in bash takes a string to execute when a signal is received. I tend to make it a function call so I can clearly define what I want done when I trap the signal.



                          Here is a sample script.



                          #!/bin/bash

                          # define the function that will handle the trap
                          sigSegFault() echo "Inside sigSegFault";

                          # Trap the Segmentation Violation (signal 11) signal
                          trap sigSegFault SEGV

                          # Start infinite loop
                          while [[ true ]]; do
                          sleep 10
                          done

                          exit 0


                          Simplistic, but contains the basics of what you are asking for.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 5 at 17:09









                          Rui F Ribeiro

                          38.6k1479128




                          38.6k1479128










                          answered Dec 5 at 16:19









                          Lewis M

                          8185




                          8185



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


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





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2funix.stackexchange.com%2fquestions%2f486180%2fconstruct-a-while-loop-around-a-command-throwing-a-segmentation-fault%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

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

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay