I'm trying to build my own hardware monitor and I'm stuck trying to put timestamps in my log file.
I'm using "ps axu" to get results and trying to use awk to filter results before sending them to a file.
Using the command below:
ps axu |awk 'NR==1 {print $0}; $3 > 0'
I get this result:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user+ 1421 0.1 0.9 304212 36384 tty2 Sl+ Oct27 4:11 /usr/lib/
user+ 1584 0.1 2.0 3626228 82152 ? Ssl Oct27 5:43 /usr/bin/
user+ 2096 0.7 14.6 4197076 587032 ? Sl Oct27 22:14 /usr/lib/
user+ 2248 0.5 8.4 3147328 340168 ? Sl Oct27 14:48 /usr/lib/
user+ 2272 1.0 14.6 3298264 587372 ? Sl Oct27 30:47 /usr/lib/
user+ 18658 0.1 0.0 12232 2468 pts/1 S+ Oct28 1:03 top
I want to send this to a new file adding one more column with a timestamp like this:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND TIMESTAMP
user+ 1421 0.1 0.9 304212 36384 tty2 Sl+ Oct27 4:11 /usr/lib/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 1584 0.1 2.0 3626228 82152 ? Ssl Oct27 5:43 /usr/bin/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 2096 0.7 14.6 4197076 587032 ? Sl Oct27 22:14 /usr/lib/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 2248 0.5 8.4 3147328 340168 ? Sl Oct27 14:48 /usr/lib/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 2272 1.0 14.6 3298264 587372 ? Sl Oct27 30:47 /usr/lib/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 18658 0.1 0.0 12232 2468 pts/1 S+ Oct28 1:03 top Thu 29 Oct 2020 09:00:42 AM EDT
I tried to use awk to do this but I'm getting some weird results when executing the command "date" and passing it as a parameter to awk.
This adds a column with "0" as result:
ps axu |awk 'NR==1 {print $0}; $3 > 0'|awk '{print $0, system(date) }'
And this adds the timestamp in a new line instead of a new column:
ps axu |awk 'NR==1 {print $0}; $3 > 0'|awk -v var1=date '{print $0 system(var1) }'