Tail: Command Line Debug

Video loading...

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

Tail: Command Line Debug


This is an introduction to the Tail command, available on Unix/Linux systems. Tail has many applications, but this video concentrates on its basic usage and useful options, as they pertain to Drupal developers.

You'll learn how to take a quick peek at recent log messages from a single log file, how to do the same thing with multiple logs, as well as watching log files in real time! We'll finish up with a practical application, to see why this is useful.

Commands used in this video:

To view the documentation (or manual) for the tail command:
man tail

To show the last 20 lines of the webserver's access log file:
tail /var/log/apache2/access.log

To show the last 20 lines of the webserver's error log file:
tail /var/log/apache2/error.log

To show the last 20 lines of the webserver's error log file and continue to print new lines added to the file:
tail -f /var/log/apache2/access.log

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.