Perl script running infinitelly - how to debug what happen?

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











up vote
1
down vote

favorite












I am running Perl script on Linux machine via cron job. However, from time to time (around 1% of all cases), the script stucks and run infinitelly. If I list processes, I can see its PID. However, I dont want to kill it right away, I would rather know what went wrong.



Is there a way, how to show what lines are being executed from the script? Something like step-by-step debugging of the script based on PID.










share|improve this question

























    up vote
    1
    down vote

    favorite












    I am running Perl script on Linux machine via cron job. However, from time to time (around 1% of all cases), the script stucks and run infinitelly. If I list processes, I can see its PID. However, I dont want to kill it right away, I would rather know what went wrong.



    Is there a way, how to show what lines are being executed from the script? Something like step-by-step debugging of the script based on PID.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am running Perl script on Linux machine via cron job. However, from time to time (around 1% of all cases), the script stucks and run infinitelly. If I list processes, I can see its PID. However, I dont want to kill it right away, I would rather know what went wrong.



      Is there a way, how to show what lines are being executed from the script? Something like step-by-step debugging of the script based on PID.










      share|improve this question













      I am running Perl script on Linux machine via cron job. However, from time to time (around 1% of all cases), the script stucks and run infinitelly. If I list processes, I can see its PID. However, I dont want to kill it right away, I would rather know what went wrong.



      Is there a way, how to show what lines are being executed from the script? Something like step-by-step debugging of the script based on PID.







      linux perl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 5 hours ago









      Martin Perry

      1236




      1236




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          6
          down vote













          Try to follow these steps:
          - find the process pid of the shell, you may use a command like:



           ps -ef | grep <your_script_name> 



          • Let's set this pid in the shell variable $PID. Find all the child processes of this $PID by run the command:



            ps --ppid $PID



          You might find one or more (if for example it's stuck in a pipelined series of commands). Repeat this command couple of times. If it doesn't change this means the script is stuck in certain command. In this case, you may attach trace command to the running child process:



           sudo strace -p $PID


          This will show you what is being executed, either indefinite loop (like reading from a pipe), or waiting on some event that never happens.



          In case you find ps --ppid $PID changes, this indicates that your script is advancing but it's stuck somewhere, e.g. local loop in the script. From the changing commands, it can give you a hint where in the script it's looping.



          More: 1, 2, 3






          share|improve this answer



























            up vote
            2
            down vote













            For the next runs of your script you can try out the package Devel::Trace.



            From description: "This module will print a message to standard error just before each line is executed."



            Run either with



            perl -d:Trace program


            or use in your script with



            import Devel::Trace 'trace';

            trace 'on'; # Enable
            trace 'off'; # Disable





            share|improve this answer




















            • But the output from the script will be captured by cron, so to actually see this you either need to look at the file descriptors used for that communication or convince cron that the script is done so it will send a mail with the output, hopefully killing it will be enough for that, but I've never tried. And as the OP said it was only about 1% of the invocations that didn't terminate in time, this could generate a lot of output before anything useful. I would probably try strace as in Goro's answer first.
              – Henrik
              4 hours ago










            • @Henrik While debugging, you can run bash -c 'perl -d:Trace program 2>&1 | tee /tmp/program.$$' to capture the output. And it doesn't matter much if many of generated files here are useless. The strace approach is universal but the output there must be harder to match with the actual Perl code.
              – Kirill Bulygin
              2 hours ago










            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%2f470681%2fperl-script-running-infinitelly-how-to-debug-what-happen%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            6
            down vote













            Try to follow these steps:
            - find the process pid of the shell, you may use a command like:



             ps -ef | grep <your_script_name> 



            • Let's set this pid in the shell variable $PID. Find all the child processes of this $PID by run the command:



              ps --ppid $PID



            You might find one or more (if for example it's stuck in a pipelined series of commands). Repeat this command couple of times. If it doesn't change this means the script is stuck in certain command. In this case, you may attach trace command to the running child process:



             sudo strace -p $PID


            This will show you what is being executed, either indefinite loop (like reading from a pipe), or waiting on some event that never happens.



            In case you find ps --ppid $PID changes, this indicates that your script is advancing but it's stuck somewhere, e.g. local loop in the script. From the changing commands, it can give you a hint where in the script it's looping.



            More: 1, 2, 3






            share|improve this answer
























              up vote
              6
              down vote













              Try to follow these steps:
              - find the process pid of the shell, you may use a command like:



               ps -ef | grep <your_script_name> 



              • Let's set this pid in the shell variable $PID. Find all the child processes of this $PID by run the command:



                ps --ppid $PID



              You might find one or more (if for example it's stuck in a pipelined series of commands). Repeat this command couple of times. If it doesn't change this means the script is stuck in certain command. In this case, you may attach trace command to the running child process:



               sudo strace -p $PID


              This will show you what is being executed, either indefinite loop (like reading from a pipe), or waiting on some event that never happens.



              In case you find ps --ppid $PID changes, this indicates that your script is advancing but it's stuck somewhere, e.g. local loop in the script. From the changing commands, it can give you a hint where in the script it's looping.



              More: 1, 2, 3






              share|improve this answer






















                up vote
                6
                down vote










                up vote
                6
                down vote









                Try to follow these steps:
                - find the process pid of the shell, you may use a command like:



                 ps -ef | grep <your_script_name> 



                • Let's set this pid in the shell variable $PID. Find all the child processes of this $PID by run the command:



                  ps --ppid $PID



                You might find one or more (if for example it's stuck in a pipelined series of commands). Repeat this command couple of times. If it doesn't change this means the script is stuck in certain command. In this case, you may attach trace command to the running child process:



                 sudo strace -p $PID


                This will show you what is being executed, either indefinite loop (like reading from a pipe), or waiting on some event that never happens.



                In case you find ps --ppid $PID changes, this indicates that your script is advancing but it's stuck somewhere, e.g. local loop in the script. From the changing commands, it can give you a hint where in the script it's looping.



                More: 1, 2, 3






                share|improve this answer












                Try to follow these steps:
                - find the process pid of the shell, you may use a command like:



                 ps -ef | grep <your_script_name> 



                • Let's set this pid in the shell variable $PID. Find all the child processes of this $PID by run the command:



                  ps --ppid $PID



                You might find one or more (if for example it's stuck in a pipelined series of commands). Repeat this command couple of times. If it doesn't change this means the script is stuck in certain command. In this case, you may attach trace command to the running child process:



                 sudo strace -p $PID


                This will show you what is being executed, either indefinite loop (like reading from a pipe), or waiting on some event that never happens.



                In case you find ps --ppid $PID changes, this indicates that your script is advancing but it's stuck somewhere, e.g. local loop in the script. From the changing commands, it can give you a hint where in the script it's looping.



                More: 1, 2, 3







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 5 hours ago









                Goro

                3,67352053




                3,67352053






















                    up vote
                    2
                    down vote













                    For the next runs of your script you can try out the package Devel::Trace.



                    From description: "This module will print a message to standard error just before each line is executed."



                    Run either with



                    perl -d:Trace program


                    or use in your script with



                    import Devel::Trace 'trace';

                    trace 'on'; # Enable
                    trace 'off'; # Disable





                    share|improve this answer




















                    • But the output from the script will be captured by cron, so to actually see this you either need to look at the file descriptors used for that communication or convince cron that the script is done so it will send a mail with the output, hopefully killing it will be enough for that, but I've never tried. And as the OP said it was only about 1% of the invocations that didn't terminate in time, this could generate a lot of output before anything useful. I would probably try strace as in Goro's answer first.
                      – Henrik
                      4 hours ago










                    • @Henrik While debugging, you can run bash -c 'perl -d:Trace program 2>&1 | tee /tmp/program.$$' to capture the output. And it doesn't matter much if many of generated files here are useless. The strace approach is universal but the output there must be harder to match with the actual Perl code.
                      – Kirill Bulygin
                      2 hours ago














                    up vote
                    2
                    down vote













                    For the next runs of your script you can try out the package Devel::Trace.



                    From description: "This module will print a message to standard error just before each line is executed."



                    Run either with



                    perl -d:Trace program


                    or use in your script with



                    import Devel::Trace 'trace';

                    trace 'on'; # Enable
                    trace 'off'; # Disable





                    share|improve this answer




















                    • But the output from the script will be captured by cron, so to actually see this you either need to look at the file descriptors used for that communication or convince cron that the script is done so it will send a mail with the output, hopefully killing it will be enough for that, but I've never tried. And as the OP said it was only about 1% of the invocations that didn't terminate in time, this could generate a lot of output before anything useful. I would probably try strace as in Goro's answer first.
                      – Henrik
                      4 hours ago










                    • @Henrik While debugging, you can run bash -c 'perl -d:Trace program 2>&1 | tee /tmp/program.$$' to capture the output. And it doesn't matter much if many of generated files here are useless. The strace approach is universal but the output there must be harder to match with the actual Perl code.
                      – Kirill Bulygin
                      2 hours ago












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    For the next runs of your script you can try out the package Devel::Trace.



                    From description: "This module will print a message to standard error just before each line is executed."



                    Run either with



                    perl -d:Trace program


                    or use in your script with



                    import Devel::Trace 'trace';

                    trace 'on'; # Enable
                    trace 'off'; # Disable





                    share|improve this answer












                    For the next runs of your script you can try out the package Devel::Trace.



                    From description: "This module will print a message to standard error just before each line is executed."



                    Run either with



                    perl -d:Trace program


                    or use in your script with



                    import Devel::Trace 'trace';

                    trace 'on'; # Enable
                    trace 'off'; # Disable






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 5 hours ago









                    Marvin

                    512




                    512











                    • But the output from the script will be captured by cron, so to actually see this you either need to look at the file descriptors used for that communication or convince cron that the script is done so it will send a mail with the output, hopefully killing it will be enough for that, but I've never tried. And as the OP said it was only about 1% of the invocations that didn't terminate in time, this could generate a lot of output before anything useful. I would probably try strace as in Goro's answer first.
                      – Henrik
                      4 hours ago










                    • @Henrik While debugging, you can run bash -c 'perl -d:Trace program 2>&1 | tee /tmp/program.$$' to capture the output. And it doesn't matter much if many of generated files here are useless. The strace approach is universal but the output there must be harder to match with the actual Perl code.
                      – Kirill Bulygin
                      2 hours ago
















                    • But the output from the script will be captured by cron, so to actually see this you either need to look at the file descriptors used for that communication or convince cron that the script is done so it will send a mail with the output, hopefully killing it will be enough for that, but I've never tried. And as the OP said it was only about 1% of the invocations that didn't terminate in time, this could generate a lot of output before anything useful. I would probably try strace as in Goro's answer first.
                      – Henrik
                      4 hours ago










                    • @Henrik While debugging, you can run bash -c 'perl -d:Trace program 2>&1 | tee /tmp/program.$$' to capture the output. And it doesn't matter much if many of generated files here are useless. The strace approach is universal but the output there must be harder to match with the actual Perl code.
                      – Kirill Bulygin
                      2 hours ago















                    But the output from the script will be captured by cron, so to actually see this you either need to look at the file descriptors used for that communication or convince cron that the script is done so it will send a mail with the output, hopefully killing it will be enough for that, but I've never tried. And as the OP said it was only about 1% of the invocations that didn't terminate in time, this could generate a lot of output before anything useful. I would probably try strace as in Goro's answer first.
                    – Henrik
                    4 hours ago




                    But the output from the script will be captured by cron, so to actually see this you either need to look at the file descriptors used for that communication or convince cron that the script is done so it will send a mail with the output, hopefully killing it will be enough for that, but I've never tried. And as the OP said it was only about 1% of the invocations that didn't terminate in time, this could generate a lot of output before anything useful. I would probably try strace as in Goro's answer first.
                    – Henrik
                    4 hours ago












                    @Henrik While debugging, you can run bash -c 'perl -d:Trace program 2>&1 | tee /tmp/program.$$' to capture the output. And it doesn't matter much if many of generated files here are useless. The strace approach is universal but the output there must be harder to match with the actual Perl code.
                    – Kirill Bulygin
                    2 hours ago




                    @Henrik While debugging, you can run bash -c 'perl -d:Trace program 2>&1 | tee /tmp/program.$$' to capture the output. And it doesn't matter much if many of generated files here are useless. The strace approach is universal but the output there must be harder to match with the actual Perl code.
                    – Kirill Bulygin
                    2 hours ago

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f470681%2fperl-script-running-infinitelly-how-to-debug-what-happen%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery