Hard-disk free space

Mini-guide - 02/01/2017

An example script notifying the user when the space used exceeds a certain threshold.

BASH script

testonoti="Low hard disk space"

current=$(df -hl | awk '/^\/dev\/sd[ab]/ { sum+=$5 } END { print sum }')

if [[ "$current" > "$soglia" ]]
  curl --request GET "$token/text/$(echo ${testonoti// /%20})"

It works in a very simple way; when the space used exceeds a set threshold a notification is sent.
You can know the percentage of the space used in several ways. It needs to be an integer value.

The variables of the script:

  • $tokenPersonal token assigned during the registration process.
  • $testonotiNotification text.
  • $currentSpace used in the hard disk.
  • $sogliaSpace threshold (in %).

The HTTP GET call structure is very simple.$token/text/$testonoti

Variable name Description Format
$token Personal token. Alphanumeric string
$testonoti Notification text. Text without spaces (use %20)

When to execute the script...?

Every 5 minutes, every hour, on every startup the script checks the hard disk situation.

You can use cron, a very popular tool to plan operations on linux systems.
Change contab with crontab -e. The syntax of the line to add is the following:

* * * * * /command/to/be/executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

For run the script on boot we can add line @reboot /command/to/be/executed on crontab file.

The asterisk (*) operator specifies all possible values for a field. For example, an asterisk in the hour field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month. In this specific example, an asterisk in all fields means run job every minute.
We can edit our crontab with crontab -e and add the line (after last line) about this script.

*/5 * * * * /home/gab/

Where /home/gab/ is the position of the script in the device.

For the execution of the script authorizations are necessary. Authorizations can be give by entering chmod 755

RpiNotify is an Italian project supported by RaspberryItaly

The first Italian RaspberryPi community.