1

How can I run the sar command to run daily using cron then get killed every 24 hours and start over again creating a new log file?

Command:

sar -u 300 288 >> $(date "+ %Y-%m-%d")-cpu.log

This will log usage every 15 minutes for just over 24 hours.

Would this be a proper bash script?

#!/bin/bash

# Kill current process
kill sar

# Start new sar
sar -u 300 290 >> $(date "+ %Y-%m-%d")-cpu.log

1 Answer 1

1

On most linux distros (if not all), sar already saves that data in a file in /var/log.... Have look at -o in man sar to find the exact path:

    -o [ filename ]
          Save the readings in the file in binary form. Each reading is in a  separate  record.  The  default
          value of the filename parameter is the current daily data file, the /var/log/sysstat/sadd file. The
          -o option is exclusive of the -f option.  All the data available from the kernel are saved  in  the
          file  (in  fact,  sar  calls  its  data collector sadc with the option "-S ALL". See sadc(8) manual
          page).

You can read that binary file using sar -f filename.

If you really need to take care of the data collection process, check man sadc.

7
  • I'm aware of that functionality, however, I need to write to a log file that's human readable for reporting cpu usage. Aug 16, 2015 at 4:13
  • @linux then just have sar >> logfile executed by cron everyday at 23:52.
    – petry
    Aug 16, 2015 at 4:19
  • Wouldn't it be advisable to kill the process in case it hasn't finished? Aug 16, 2015 at 4:20
  • @linguru772x if you running sar without interval or count, it works more like cat. It writes to standard output and terminates, so you have no process to kill... If you're running it with count, it will just stop after it finishes counting. Try running sar and sar 2 2 to see the difference.
    – petry
    Aug 16, 2015 at 4:34
  • so sar -u 900 288 would run from 12:05AM to 12:00AM (next day) if kicked off at 0 0 * * * in cron? Aug 16, 2015 at 4:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .