Create a cron job that monitors disk space usage and sends a notification if it exceeds specified threshold in Linux
Social Share:
Thursday, August 8, 2024 at 4:46 AM | 3 min read
Last modified on Thursday, July 16, 2026 at 8:59 PM
#cron daemon, #crontab, #linux, #automation, #task automation

Photo by Ksenia Chernaya on pexels.com
Table of Contents
- Creating the disk_space_alert.sh file
- Adding the shell script to disk_space_alert.sh
- The df command
- The awk command
- The tr command
- if/else statement
- Adding the disk_space_alert.sh shell script to crontab
- Conclusion
- Related Resources
I run my Linux Mint distribution in VirtualBox, so the virtual disk space I have allocated to it is not as much as I would like. I want to be able to have hard disk space allocated to my Windows 11 partition as well. That means I should be monitoring the disk space that Linux Mint in VirtualBox is eating up, so I created a cron job to do just that.
Creating the disk_space_alert.sh file
First, I created an executable file called disk_space_alert.sh and set executable permissions on it:
chmod +x disk_space_alert.sh
chmod stands for "change mode".
Then I check that chmod added execute permissions to disk_space_alert.sh by running the ls -l command in Terminal, and the following is returned as a result:
-rwxrwxr-x 2 maria maria 206 Aug 8 09:12 disk_space_alert.sh
Adding the shell script to disk_space_alert.sh
Then I add the following script to disk_space_alert.sh:
#!/bin/bash threshold=90 current_usage=$(df -h / | awk 'NR==2 {print $5}' | tr -d '%') if [ "$current_usage" -ge "$threshold" ] then echo "Disk space is running low!" else echo "Disk space is okay." fi
The df command
df refers to the "disk free" command. The -h flag means "human readable", and / refers to the root directory of the filesystem hierarchy which the df command searches through and returns the percentage of disk used. To learn more about the filesystem hierarchy, please read my article entitled The Linux Filesystem Hierarchy.
df -h / is piped into the awk command as stdin:
current_usage=$(df -h / | awk 'NR==2 {print $5}' | tr -d '%')
To learn more about the df command, run man df in Terminal.
The awk command
awk is an interpreter for the AWK programming language and stands for "Aho, Weinberger, and Kernighan", who were the designers of the AWK language. It originated at AT&T Bell Laboratories in 1977. mawk is an awk interpreter used for manipulating data files and text retrieval and processing.
NR stands for "Number of Records", and it is a built-in awk variable. It means "number of line" or "number of current line". NR keeps a current count of the number of input records.
awk 'NR==2 {print $5}' means if NR is equal to 2, print field (or column) $5.
Then awk 'NR==2 {print $5}' is redirected as stdin to tr -d '%'.
The tr command
The tr command translates, squeezes, and/or deletes characters from stdin, writing to stdout. It requires at least one string known as STRING1. STRING1 specifies the characters to be translated or deleted.
The basic tr command syntax is:
tr [OPTIONS] STRING1 [STRING2]
STRING1 represents the characters to be translated or deleted.
STRING2 represents the set of characters that will replace the characters in STRING1 if translation is specified.
OPTIONS refers to the flags that modify the tr command behavior.
tr -d '%' deletes the '%' symbol so that the value of current_usage is an integer.
if/else statement
The meaning of the "if/else" statement is if "$current_usage" is greater than or equal to (-ge) "$threshold", which is 90, then echo "Disk space is running low!", else echo "Disk space is okay."
Adding the disk_space_alert.sh shell script to crontab
Lastly, I add this shell script to the bottom of my crontab file:
# first I have to run crontab -e in Terminal to open up disk_space_alert.sh crontab -e # then I add the following to the bottom of the crontab file 30 06 * * * /home/maria/Desktop/cron-job-scripts/disk_space_alert.sh
After the cron job is executed, I receive a local email via mailx telling me either "Disk space is running low!" or "Disk space is okay.".
To learn how to create a cron job in Linux, please visit my article entitled The Cron daemon in Linux and how to create a cron job.
To learn about the mailx command and local emails in Linux, please visit my article entitled How to send/receive local emails in Linux Mint/VirtualBox.
Conclusion
In this post, I talk about how I create a cron job that monitors the disk space usage on my computer and automatically sends me an email if the usage exceeds a specified threshold. To achieve this, I create a disk space usage monitoring script in which I use the df, awk, and tr commands. After I create the script, I add it to the bottom of my crontab file, which contains a list of cron jobs and when they should be executed. Once the cron job is executed, I receive an email that either states "Disk space is running low!" or "Disk space is okay." This way I don't have to manually run a command over and over again to find out whether my disk space is low or not!
Related Resources
- What is awk?: opensource.com
- Tr Command In Linux Explained With Examples: ostechnix.com
- Tr Command In Linux Explained With Examples For Beginners: Linux Today
- tr(1) — Linux manual page: man7.org
- 9.1 tr: Translate, squeeze, and/or delete characters: gnu.org
- How to use df command in Linux / Unix {with examples}: cyberciti.biz
- awk(1p) — Linux manual page: man7.org