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



Perl script running infinitelly - how to debug what happen?



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.




2 Answers
2



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.


ps --ppid $PID



More: 1, 2, 3



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





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


strace





@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


bash -c 'perl -d:Trace program 2>&1 | tee /tmp/program.$$'


strace






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS