-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 Answers1

1

You were close: the system function requires a string

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

reference

glenn jackman
  • 4,320
  • 16
  • 19
  • 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? – PatrickDickey Apr 12 '15 at 02:37
  • No. you need the quotes as shown. Cron is not that magical that it changes how awk works. – glenn jackman Apr 12 '15 at 11:39
  • So my cron job would say @daily 'df -P /dev/sda2 | tail -1 | awk '$5 > 90 {logrotate -f /etc/logrotate.d}' ? – PatrickDickey Apr 14 '15 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")}'` – glenn jackman Apr 14 '15 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. – PatrickDickey Apr 16 '15 at 20:08