I would like to track total CPU and memory use on an Ubuntu 20.04 machine. For that purpose, I am about to install the sysstat library. I read the manuals of and posts on sar and related functions, but am still confused about the syntax.

What I would like to do: Record total CPU and memory use every five seconds. Export the data once a day as CSV and delete the binary data.

What confuses me: 1. There are multiple settings specifying time intervals. sar -u %system -r %memused 5 records CPU and memory use every five seconds. On top of that, the package generates "activity reports" every ten minutes as defined by 5-55/10 * * * * root command -v debian-sa1 > /dev/null && debian-sa1 1 1 in /etc/cron.d/sysstat. I am confused about what happens every five seconds and what happens every ten minutes. Does the program first cache the data every five seconds and then writes it to a file every ten minutes? 2. Multiple functions write to disk. Which of them do I need? sar -u %system -r %memused -o 5, sadc -u %system -r %memused 5 -, or sa1? 3. Do I need to execute the code that generates a daily CSV file (sadf -d /var/log/sysstat/sa$(date +%d -d yesterday) > /home/some_user/sar_data.csv) as a chron job?

As a side note: if anyone knows of a solution that would be computationally more efficient than this sysstat approach, feel free to suggest it.

  1. 5-55/10 runs the command at 5,15,25,35,45, and 55 seconds.
  2. debian-sa1 1 1 is debian-sa1 [ interval [ count ] ] so it will collect for one second one time.

In your sar command you do not have a count so it will accumulate for 5 seconds until stopped.

While it will significantly increase log size and CPU usage, as cron(8) examines cron entries every minute, .

   The time and date fields are:

          field          allowed values
          -----          --------------
          minute         0-59
          hour           0-23
          day of month   1-31
          month          1-12 (or names, see below)
          day of week    0-7 (0 or 7 is Sunday, or use names) 

As cron runs into serious problems if the job takes longer to run than the interval between launches, it is unsafe to just adjust the cron entry to:

*/1 * * * * root command -v debian-sa1 > /dev/null && debian-sa1 10 5


*/1 * * * * root for i in `seq 1 5` ; do debian-sa1 1 1 ; sleep 10 ; done

But doing so as a test may show just how much CPU time and disk space this option will be. But the second command will have slightly less impact as it is only collecting for 1 second out of 10.

But the log files will be in similar size for either of above.

as neither systat nor cron are designed for that frequency of collection do not expect high stability of the granularity in the above.

  • Thanks, that is already helpful. Would you mind elaborating on a few details? 1. Are you sure that 5-55/10 refers to the seconds? I thought it refers to minutes. 2. I still did not fully understand what the different time references at the beginning and the end of the cron job imply. If I used 0 0 * * * root command -v debian-sa1 > /dev/null && debian-sa1 1 1, it would record system stats only once a day at midnight, right?
    – Chr
    Jul 18 at 20:43
  • If I used 0 0 * * * root command -v debian-sa1 > /dev/null && debian-sa1 5 43200, it would run 43200 queries over 5 seconds at midnight rather than 1 query every 5 seconds over a day starting at midnight, right? How do I get it to start once a day and keep collecting data once every five seconds? And where would I specify the -u %system -r %memused options in that sa1 line?
    – Chr
    Jul 18 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.