in reply to Re: Emulating command line pipe
in thread Emulating command line pipe

Thank you for your suggestion. This works fine in a script that gets executed at the command line, but when run as a cgi I see processes get split up, which prevents the -m 1 grep argument from killing tac:

apache 2717 0.1 0.1 5112 3356 ? S 12:42 0:00 /usr/ +bin/perl /w3/mysite.com/myapp/cgi-bin/myapp_sessions.cgi apache 2920 0.0 0.0 2296 1024 ? S 12:42 0:00 sh -c + tac /var/log/httpd/my_big_access_log | grep -m 1 /myapp/ apache 2921 24.9 0.0 1572 408 ? R 12:42 0:07 tac / +var/log/httpd/my_big_access_log
Here's my CGI version, would you have any other advice by chance?

Thank You,
Troy

---
#!/usr/bin/perl use strict; use CGI; use Date::Parse; my $log_dir = '/var/log/httpd/'; my $session_limit = 3600; # 1 hour my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime +(time); $year += 1900; $month++; my $date_cmp_str = join('', ($year, sprintf('%02d', $month), sprintf(' +%02d', $mday), $hour, $min, $sec)); my $cgiq = new CGI; print $cgiq->header(); print '<html><head><title>myapp user status</title><link rel="styleshe +et" href="/style/login.css" type="text/css" />'; print '<style type="text/css">li { font-size: 14px; }</style></head><b +ody><h1>myapp user status</h1>'; print "\n<h2>Last known user activity:</h2>\n<ul>\n"; my @expired; my @active; opendir(DIR, $log_dir); my @files = grep { /_access_log$/ } readdir(DIR); closedir DIR; print STDERR "Files = " . join(", ", @files) . "\n\n"; sleep(3); foreach my $lf (@files) { # make sure this is an myapp site my ($domain, $t1, $t2) = split("_", $lf); my $myapp_dir = '/w3/' . $domain . '/myapp/'; if (!-e $myapp_dir) { next; } else { # Find last login my $file = $log_dir . $lf; my $last_line = ''; my $pid = open IN, "tac $file | grep -m 1 /myapp/ |" or die $! +; while( <IN> ) { $last_line = $_; } close IN; if ($last_line eq '') { # print "No login in logs.\n"; } else { my ($ip, $d1, $d2, $date, $tz, $method, $url, $rest) = spl +it(" ", $last_line); $date = substr($date, 1); $tz = substr($tz, 0, -1); my $cdate = $date . ' ' . $tz; my ($ss,$mm,$hh,$dy,$mon,$yr,$zone) = strptime($cdate); $yr += 1900; $mon++; my $date_str = join('', ($yr, sprintf('%02d', $mon), sprin +tf('%02d', $dy), $hh, $mm, $ss)); my ($domain, $t3, $t4) = split("_", $lf); if ($date_str > ($date_cmp_str - $session_limit)) { print "<li><font color='red'>User logged in! <a href=' +http://$domain'>$domain</a> last hit $mon/$dy/$yr $hh:$mm:$ss by IP $ +ip</font></li>\n"; } else { print "<li>$domain last hit $mon/$dy/$yr $hh:$mm:$ss b +y IP $ip</li>\n"; } } } } print "</ul></body></html>";

Replies are listed 'Best First'.
Re^3: Emulating command line pipe
by BrowserUk (Patriarch) on Aug 31, 2007 at 17:05 UTC