Sub topic 12
Running a Disk and Inode Check on a Schedule
This chapter assumes that /usr/local/bin/check-disk.sh already exists, is executable, and accepts an optional percentage threshold. The script should print a warning when space or inode usage reaches that threshold.
A scheduled check turns a one-time diagnostic into an early warning system. The scheduler starts the script automatically. You then inspect its output or send that output to your monitoring system.
Choose a scheduler
Cron is a simple scheduler available on most Linux systems. A cron entry contains five time fields followed by a command:
minute hour day-of-month month day-of-week commandFor example, this entry runs the check every 15 minutes:
*/15 * * * * /usr/local/bin/check-disk.sh 85 2>&1 | logger -t check-diskThe */15 value means every 15 minutes. The remaining asterisks mean every hour, day, month, and weekday. 2>&1 combines error output with normal output. The logger command writes the result to the system log with the tag check-disk.
Edit the current user's crontab with:
crontab -eIf the script needs root access to read all mounts, install the entry in root's crontab instead:
sudo crontab -eThe root crontab runs commands as root, so use it only when the script requires those permissions.
Understand cron's environment
Cron runs with a smaller environment than an interactive SSH shell. It may use a different PATH, working directory, shell, and home directory. Use absolute paths in scripts and cron entries. Do not assume that aliases, shell startup files, or an SSH agent are available.
Set permissions and test the exact command before scheduling it:
sudo chmod 0755 /usr/local/bin/check-disk.sh
sudo /usr/local/bin/check-disk.sh 85
sudo /usr/local/bin/check-disk.sh 85 2>&1 | logger -t check-diskThen inspect recent messages:
sudo journalctl -t check-disk --since "10 minutes ago"If the script produces no warnings, the log may contain no entry. For testing, temporarily lower the threshold, such as 1, and restore the intended value afterward.
Use a systemd timer
On a system using systemd, a timer can make scheduling and logging easier to inspect. Create /etc/systemd/system/check-disk.service:
[Unit]
Description=Check filesystem space and inodes
[Service]
Type=oneshot
ExecStart=/usr/local/bin/check-disk.sh 85Create /etc/systemd/system/check-disk.timer:
[Unit]
Description=Run the disk check every 15 minutes
[Timer]
OnBootSec=5min
OnUnitActiveSec=15min
Persistent=true
[Install]
WantedBy=timers.targetLoad and enable the timer:
sudo systemctl daemon-reload
sudo systemctl enable --now check-disk.timer
sudo systemctl list-timers check-disk.timerOnBootSec schedules the first run five minutes after boot. OnUnitActiveSec schedules another run 15 minutes after the service finishes. Persistent=true allows systemd to run a missed activation after the machine was powered off.
View service output and failures with:
sudo systemctl start check-disk.service
sudo systemctl status check-disk.service
sudo journalctl -u check-disk.service --since "1 hour ago"Select and verify the schedule
Cron is convenient for a short command. A systemd timer gives you named units, journal integration, status output, and clearer failure reporting. Use one scheduler for this check, not both, or the script will run twice as often as intended.
Exercise
Why should a scheduled disk-check script use absolute command paths or define a known PATH?
Show answer
Cron and systemd do not necessarily load the same shell environment as an SSH session. Without a known PATH, a command may not be found or may resolve differently.