Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2896  / 3 Years ago, sun, august 8, 2021, 1:01:01

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.


More From » cron

 Answers
7

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.


[#26288] Sunday, August 8, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
weamp

Total Points: 197
Total Questions: 115
Total Answers: 92

Location: Mauritania
Member since Sun, May 7, 2023
1 Year ago
;