Jump to content

Can I run a PHP script from the CLI, and have the terminal show me the current line number in real time?


Recommended Posts

I have this script that is behaving in an unexpected way. I've looked at it over and over and I just can't figure out where its going wrong. But its not the kind of thing I could easily post here to have other eyes on. This script is getting some data from HTTP requests and getting some data from SQL and its running shell commands, all the kind of stuff thats hard to recreate in something like a code-testing website. 

So now I'm wondering, is there a way I can run a script in some kind of a quasi-debug mode, where it will show me the line the script is on in the terminal, in real time? That may, and probably will, give me a big clue as to whats going on when this script behaves strangely. 

And if you're wondering, this is kind of an important script. It is my last line of defense anti-dos script. It runs very regularly, checks if the server is online, by actually making some http requests to some files I have stored on other servers. Then if the server is offline, it resets the TCP stack every 5 minutes that the server is still offline, and it automatically reboots the server every 15 minutes that the server is offline. This script has saved my ass many times. My next server is going to have much better defenses against this kind of thing, and hopefully this script never does anything on that machine. But for now, this script is my savior. And it is doing it's job, it just has this strange habit of thinking the server is still offline, and re-resetting the TCP stack a second time.

If I had to guess, I'd say the problem is probably an HTTP request that is getting hung up, and because its getting hung up on the "make a connection" phase, its time doesn't count towards timeouts, so it just hangs for long enough that it things its time to reset again. That's my guess. But I don't want to guess, I want to know. Hence line numbers. 

The script isn't all that long. So I could do something like add a function that writes __LINE__ to a log file, at the start of every single line of the script. But maybe theres an easier way that doesn't require installing extra software? 

For CLI debugging you have several options:

  • Add debugging statements to the code and log them using a library like monolog
  • Use a debugger

With debugging you have the options of:

  • Using XDebug
    • Must have the XDebug extension installed and configured
    • Most likely have done some setup with your IDE of choice
  • Using phpdbg

Phpdbg is the much lesser known option, and has been bundled with php since php 5.  It essentially does by default what you are asking for.

You load using:  phpdbg -e your_script.php

If it's available it will load the program into its console and from there you can run the script, stepping through the code, and looking at variables.  There's an old tutorial with some examples here:  https://samtuke.com/2014/07/phpdbg-debugger-quickstart-guide-and-practical-uses/

If it doesn't work for you, then you can still fall back to XDebug.   With that said, the old techniques of adding var_dump or print_r statements along with die() or logging statements to files should not be discounted.

 

 

I just did it manually. Added a "function(__LINE__);" to every line of the script, and that function output the line number to a log file, and i 'tail'ed that file while running the script, and the script was hanging up where i suspected it was. Now to figure THAT out 😕

10 hours ago, l008com said:

I just did it manually. Added a "function(__LINE__);" to every line of the script, and that function output the line number to a log file, and i 'tail'ed that file while running the script, and the script was hanging up where i suspected it was. Now to figure THAT out 😕

Glad to hear it.  Did you by chance try to see if you could run it through phpdbg?  It automagically displays line numbers in a variety of ways. 

Yeah apparently my install doesn't have phpdbg and my package manager also doesn't? My production server has it but not my desktop where i'm just messing around with stuff. 

2 hours ago, l008com said:

Yeah apparently my install doesn't have phpdbg and my package manager also doesn't? My production server has it but not my desktop where i'm just messing around with stuff. 

It comes with any cli install.  I'm not sure if you are using docker or apache+mod_php or something like that, or php isn't in your path.  There are many possible configurations, but I understand it can be confusing.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.