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.
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 the raw 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.