debugging cgi using tail
use tail
Tail outputs the last part of a file, by default usually the last ten lines.
Use tail via the command line, that is, you can view all your cgi script’s output to STDERR real time to the command line as it runs. This is crucial with cgi, because you only see STDOUT the client browser ( yes I know, don’t be a smartass).
If your script is called via http, your log file is usually in /var/log/httpd/error_log, if you’re using https (ssl), then your logfile is in /var/log/httpd/ssl_error_log
The most easy way to do this is to tail follow..
tail -f /var/log/httpd/error_log
At this point you’ll notice the prompt is not returned back to you, it looks as if it hangs. If your script outputs to STDERR.. run it and watch the terminal.
For example:
#!/usr/bin/perl
use CGI;
my $cgi = new CGI;
print $cgi->header;
print $cgi->h1("Welcome");
print STDERR "This script ran.";
You’ll notice every time the script is run, you see it output “Super” to the apache log file (httpd is the http daemon, this is apache, the files residing in /var/log/httpd are apache log files).
more involved usage of tail on logfile
Now, I do a lot of debugging on them nasty cgi.
I don’t like seeing through all the freaking timestamp and referrer info.. I just want to see the output.
You may be using this off and on, so create a script in # vim /home/bin/httperrs, we’ll use bash for this one..
#!/bin/sh tail -f /var/log/httpd/error_log | perl -p -e 's/^.+\d+\.\d+\.\d+\.\d+\]|\, referer\:.+$//g'
This takes out the ip address, time, and the trailing referer, from standard httpd error log entries. (We could have used sed for this also.)
