Example using GNU `time` to track usage - AstrobioMike/JPL-HBCU-2020 GitHub Wiki

The GNU time command can help us track information we want while we are running our script. The full manual page can be found here.

First, here is an example just with ls as the program we are keeping track of:

command time -f "\t%E real, \t%U user, \t%S sys, \t%M max_mem, \t%K av_mem, \t%P cpu" -o usage-info.txt ls

Where:

  • command time – specifies the proper time command we want to use
  • -f "..." – specifies the format of the output of the command we want
  • -o usage-info.txt – tells the program to write the output information to this file we are naming here
    • this part is the "end" of the command time part, following this is the command we are running that we want to capture the information for
  • ls – is an example of the program we are running that we want to capture the information for

That created a file called "usage-info.txt" we can look at:

cat usage-info.txt

Which contains our output of what we were tracking:

#	0:00.00 real, 	0.00 user, 	0.00 sys, 	2580 max_mem, 	0 av_mem, 	66% cpu

And here is an example when we are running a script we've put together, here just called "my-script.sh":

command time -f "\t%E real, \t%U user, \t%S sys, \t%M max_mem, \t%K av_mem, \t%P cpu" -o usage-info.txt bash my-script.sh
  • command time – specifies the proper time command we want to use
  • -f "..." – specifies the format of the output of the command we want
  • -o usage-info.txt – tells the program to write the output information to this file we are naming here
    • this part is the "end" of the command time part, following this is the command we are running that we want to capture the information for
  • bash my-script.sh – is an example of the program we are running, the "my-script.sh" will be replaced by whatever script we want to run