Is there a smart way to execute a script when disk space is low? I know I can manually check disk space, but it seems like a common problem to want to delete log files when disk space is low (for example) which is what I want to do.

2 Answers 2


You can use df and a one-line shell script.

First, we need to tell df to only print the percentage used:

df / --output='pcent'

Combine this with grep to get only the number:

df / --output='pcent' | grep -o "[0-9]*"    

This will yield e.g. "55" if the disk has been filled by 55%.

Now in the crontab, we can use it like this:

@daily sh -c "if [ $(df / --output='pcent' | grep -o '[0-9]*') -gt 90 ]; then docker system prune -af; fi"

In this example, we run docker system prune -af if the disk has been filled by more than 90%. Adjust as needed.


If you are not specifically concerned about your log files, you could put a script to check the disk space in the cron.

Automatically clearing old logs is what logrotate is for.


You must log in to answer this question.

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