How do I append the current system time of the time of execution of a sh script, to an output file in bash?

22 Views Asked by At

I am gathering data about CPU and memory usage on my computer every 15 minutes for a period of 24 hours.

I used the ps command from Ubuntu manuals to retrieve the attributes of interest (user, time, command, drs etc...). I echo the bash command, including the she-bang, into a .sh file, which appends the results into the "output.txt" file:

!echo -e '#!/bin/bash\n'ps -eo user,cputimes,comm,drs,rss,vsz,lstart --sort -cputimes '>> output.txt' > script.sh

I then execute the script to run every 15 minutes over a 24hr period until it self-terminates:

!timeout 1440m watch -n900 sh script.sh

The script runs correctly and returns my desired attributes (although I might want to specify how the text file is delineated in future attempts):

USER         TIME COMMAND           DRS   RSS    VSZ                  STARTED
hpnunes      4799 gnome-shell     5530046 180844 5530052 Fri Jul 29 18:05:00 2022
hpnunes      3084 Xwayland        491848 97336 493408 Fri Jul 29 18:05:00 2022
hpnunes      1770 Isolated Web Co 3040881 290648 3041488 Mon Aug  1 10:50:45 2022

What I want is an additional attribute field which registers the current system time of when the script is being executed every 15 mins, as returned by the bash command 'date' for example.

0

There are 0 best solutions below