Tail: Command Line Debug
0 (0 Likes / 0 Dislikes)
Tail: Command Line Debug
with Will Hetherington
In this video, we are going to
discuss the tail command.
It's a Unix tool, and by default
prints the last 10 lines of input
you provide to standard output,
which makes it really useful for debugging.
This might have you asking,
what is standard output?
Well, by default this is your terminal window.
In documentation, you'll often see
this shortened to just "stdout," standard out.
Today we're going to use tail to examine some log files and then to watch those logs in real time
and then a practical application using tail to debug a white screen of death with a Drupal install.
So let's take a look at the most basic example.
By default my web server's log files are located
in /var/log/apache2. And if we take a look at the access log, by default tail will print the last 10 lines
from the file you provide. Let's take a quick moment to look at the manual page, which you can open
by typing "man tail" at your terminal. I'm not going to discuss all of the options that you could pass to tail,
just the two that I find most useful. That would be -n or --lines. And you can pass an integer with the number of lines
you'd like to return. The other is -f or --follow, and this will keep showing output as the file grows.
So let's take a look at examples of both of those. If we take the -n flag and pass, say, the number 50,
then we're going to get 50 lines of output back from tail, or rather tail's going to provide 50 lines of output
from the file that we passed to it if indeed 50 lines exist in the file. Otherwise it will show as many as it can.
My other favorite is -f or --follow. So the -f flag or --follow, it's really useful for watching your log files
in real time. So you'll notice at the bottom of my terminal, I don't have a prompt currently.
I do have a flashing cursor but no prompt. And the reason for that is that using the -f flag means that
tail will keep the file that we passed open to it, and it will keep printing the output to the terminal window
as soon as it's written to the file. So to show that in action, I am just going to refresh the browser
on the left-hand side a couple of times, and you'll see the log rows on the right-hand side happily being printed
by tail. Let's say, for a second, that you're developing a new module for your Drupal install.
Everything's been going swimmingly until you press F5 or click refresh on your browser, and the dreaded white screen of death appears.
At this point you probably don't have much idea about the problem that you've arrived at.
This is where tail can really come to the rescue. So first things first. We're going to want to take a look
at our web server's error log. This is most often, when you arrive at a white screen of death, where you'll find
more information. Running the tail command and the path to our error.log, you will be able to see
the last 10 lines of output which will hopefully give us some clues as to what the problem might be.
So just by reading the last couple of lines in this error.log, you'll see that it says the allowed memory size
has been exhausted. So this is most likely an issue with the memory limit in your PHP configuration.
Now to be clear, tail doesn't try and help us fix the problem. It really helps us to pinpoint or narrow down
what or where it might be. In this case, my issue was a problem with PHP's max memory,
and after increasing the allowed max memory, I've restarted my web server, and my site is back in action.
So in this video, we've covered examining your access and your error logs with tail, also how to watch those log files
in real time with tail, and then a practical application where we use tail to analyze a white screen of death
from a Drupal install from a log perspective. Thanks for watching.