From e58109711331109aa7eff5e1459dbfce57d97916 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Tue, 30 Jul 2019 23:05:07 -0700 Subject: [PATCH] Show start time nicely formatted https://github.com/authentic-theme/authentic-theme/issues/1387 --- proc/index_tree.cgi | 2 +- proc/index_user.cgi | 2 +- proc/linux-lib.pl | 12 ++++++++++++ proc/proc-lib.pl | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/proc/index_tree.cgi b/proc/index_tree.cgi index e221ce790..fa74d062f 100755 --- a/proc/index_tree.cgi +++ b/proc/index_tree.cgi @@ -16,7 +16,7 @@ foreach $pr (@procs) { $procmap{$p} = $pr; $argmap{$p} = $pr->{'args'}; $usermap{$p} = $pr->{'user'}; - $stimemap{$p} = $pr->{'_stime'}; + $stimemap{$p} = &format_stime($pr); push(@{$children{$pp}}, $p); $inlist{$pr->{'pid'}}++; } diff --git a/proc/index_user.cgi b/proc/index_user.cgi index fde13040f..7b7cdcaaf 100755 --- a/proc/index_user.cgi +++ b/proc/index_user.cgi @@ -30,7 +30,7 @@ foreach $u (@users) { } push(@cols, $pr->{'cpu'}); if ($info_arg_map{'_stime'}) { - push(@cols, $pr->{'_stime'}); + push(@cols, &format_stime($pr)); } push(@cols, &html_escape(&cut_string($pr->{'args'}))); print &ui_columns_row(\@cols); diff --git a/proc/linux-lib.pl b/proc/linux-lib.pl index d26140193..19174030a 100755 --- a/proc/linux-lib.pl +++ b/proc/linux-lib.pl @@ -28,6 +28,7 @@ if ($ver >= 2) { } open(PS, "ps --cols 2048 -eo user$width,ruser$width,group$width,rgroup$width,pid,ppid,pgid,pcpu,vsz,nice,etime,time,stime,tty,args 2>/dev/null |"); $dummy = ; + my @now = localtime(time()); for($i=0; $line=; $i++) { chop($line); $line =~ s/^\s+//g; @@ -52,6 +53,17 @@ if ($ver >= 2) { $plist[$i]->{"bytes"} = $w[8]*1024; $plist[$i]->{"time"} = $w[11]; $plist[$i]->{"_stime"} = $w[12]; + if ($w[12] =~ /^(\d+):(\d+)$/ || + $w[12] =~ /^(\d+):(\d+):(\d+)$/) { + # Started today + $plist[$i]->{"_stime_unix"} = + timelocal($3 || 0, $2, $1, $now[3], $now[4], $now[5]); + } + elsif ($w[12] =~ /^(\S\S\S)\s*(\d+)$/) { + # Started on some other day + $plist[$i]->{"_stime_unix"} = + timelocal(0, 0, 0, $2, &month_to_number($1), $now[5]); + } $plist[$i]->{"nice"} = $w[9]; $plist[$i]->{"args"} = @w<15 ? "defunct" : join(' ', @w[14..$#w]); $plist[$i]->{"_group"} = $w[2]; diff --git a/proc/proc-lib.pl b/proc/proc-lib.pl index f8f934b05..925e892ef 100755 --- a/proc/proc-lib.pl +++ b/proc/proc-lib.pl @@ -641,5 +641,21 @@ else { } } +# format_stime(&proc) +# Returns the process start time in human-readable format +sub format_stime +{ +my ($p) = @_; +if (!$p->{'_stime_unix'}) { + return $p->{'_stime'} + } +elsif (time() - $p->{'_stime_unix'} > 86400) { + return &make_date($p->{'_stime_unix'}, 1); + } +else { + return &make_date($p->{'_stime_unix'}); + } +} + 1;