Sokonomi
Contributor
- Joined
- Jul 15, 2018
- Messages
- 115
I'm a complete novice to the nix verse, so all of this is some pretty hairy territory for me;
What I'm trying to do, is to get a cronjob to occasionally delete the oldest folder in a dataset, whenever it detects free space dropping below a threshold. I believe pruning is the word for this function. I've struggled through some google hits and tutorials and managed to write a bash script.. I think.
I've commented out the actual delete command since I am not sure how to point this thing in the right folder yet, but it should be spitting out some info when you run it, I think. But here's the issue; it doesn't.
From what I can gather, bash should be installed out of the box. I'm trying to execute the script from Shell, as root. Whenever I run the script it just drops back to command without echoing anything, zero feedback. Does anyone know what could be the issue?
I'm running TrueNAS-12.0-U8.1
What I'm trying to do, is to get a cronjob to occasionally delete the oldest folder in a dataset, whenever it detects free space dropping below a threshold. I believe pruning is the word for this function. I've struggled through some google hits and tutorials and managed to write a bash script.. I think.
Code:
```bash #!/usr/bin/env sh # Set the minimum free disk space in gigabytes MIN_FREE_SPACE_GB=1 # Get the current free disk space in bytes free_space=$(df -Pk . | awk 'END{print $4}') # Convert the free space from kilobytes to gigabytes free_space_gb=$((free_space / 1024 / 1024)) # Check if the free space is below the minimum threshold if [ "$free_space_gb" -lt "$MIN_FREE_SPACE_GB" ]; then # Find the oldest directory and delete it oldest_dir=$(ls -t1d */ | head -n 1) # rm -rf "$oldest_dir" echo "Deleted $oldest_dir as free space was below $MIN_FREE_SPACE_GB GB." else echo "Sufficient free space available." fi ```
I've commented out the actual delete command since I am not sure how to point this thing in the right folder yet, but it should be spitting out some info when you run it, I think. But here's the issue; it doesn't.
From what I can gather, bash should be installed out of the box. I'm trying to execute the script from Shell, as root. Whenever I run the script it just drops back to command without echoing anything, zero feedback. Does anyone know what could be the issue?
I'm running TrueNAS-12.0-U8.1