Linux Directory Checking Script

Morning! Oh wait, it’s the afternoon…

Today, I finally got around to making a script that will run automatically on my network storage server (Raspberry Pi with a dinky USB hard drive) and check if the USB HDD is accessible.

This issue started a couple of weeks ago where I was getting weird IO errors on the USB disk about every 2 weeks. Instead of buying a new drive, creating a RAID array or anything else equally as intelligent and appropriate, I decided to just reboot my Raspberry Pi every time this happened. Now, I don’t want to do this manually every time so I finally created a script and added it to my cron jobs.

You can see the script I used below:

#!/bin/bash

if [ ! -d "path/to/check" ]; then
    #Directory is not found and HDD is not okay, do whatever is below
    uptime=$(uptime)
    currenttime=$(date)
    echo "Host rebooted at $currenttime. Uptime was$uptime" >> /path/to/output.txt
    sudo reboot
fi

My crontab job is running as root because the sudo reboot part was giving me a couple of issues. This is the entry in the root crontab:

@hourly /path/to/sh/file

Enjoy!

Leave a Comment

Your email address will not be published. Required fields are marked *

email popup image
Mark Harwood
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Subscribe
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Fill Out This Form, And I Will Be In Touch Shortly
Contact form image
I'll Be In Touch Soon