-1

What I'm trying to do is manually run logrotate when my disk is greater than 90% full. I'm trying to do this using df -P and awk. I can get it to display the percentage, if it's > 90, but I can't quite figure out how to get awk to run logrotate -f /etc/logrotate.d/ if it's > 90.

Here's what I have that works so far:

Printing the percentage if it's > 90: df -P /dev/sda2 | tail -1 | awk '$5 > 90 {print $5}'

Trying to run logrotate

  df -P /dev/sda2 | tail -1 | awk '$5 > 90 {system(logrotate -vf /etc/logrotate.d) /dev/sda2}'
  df -P /dev/sda2 | tail -1 | awk '$5 > 90 {logrotate -f}' 

I'm new to all of this, so the exact command would be greatly appreciated. My intent is to put this into a shell script, or as the command portion of a daily cron job.

Thanks for the help. Patrick.

1 Answer 1

1

You were close: the system function requires a string

awk '$5 > 90 {system("logrotate -vf /etc/logrotate.d")}'

reference

5
  • Thanks. That works. If I wanted to add that to a crontab, would something like [code]@daily ' df -P /dev/sda2 | tail -1 | awk '$5 > 90 {logrotate -f}'df -P /dev/sda2 | tail -1 | awk '$5 > 90 {logrotate -f}'[/code] (without the "") work? Apr 12, 2015 at 2:37
  • No. you need the quotes as shown. Cron is not that magical that it changes how awk works. Apr 12, 2015 at 11:39
  • So my cron job would say @daily 'df -P /dev/sda2 | tail -1 | awk '$5 > 90 {logrotate -f /etc/logrotate.d}' ? Apr 14, 2015 at 12:45
  • You seem determined to not understand what I wrote: @daily df -P /dev/sda2 | tail -1 | awk '$5 > 90 {system("logrotate -vf /etc/logrotate.d")}' Apr 14, 2015 at 13:08
  • I'd love to claim it was a severe lack of sleep, but it really wasn't. I was thinking that in order to make the cron work, I'd have to surround the entire command with quotes as well. Thank you for your patience and help. Apr 16, 2015 at 20:08

You must log in to answer this question.

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