Page 1 of 1

Script to check for media files

Unread postPosted: 30 Mar 2015, 01:55
by gurutech
Last week at some point, my server lost its connection to the computer I have my media stored on, and I didn't even know it until I went to listen to some tunes and the Madsonic client on my Android couldn't find any music. Of course, I was away from home, and where I was blocks access to the tool I use to access my network remotely, so I was stuck (at work) with no tunes!

So I created a small little script to check my media files every 30 minutes to make sure Madsonic can still access them. If it can't access them, it sends me an email to let me know.

The script comes in a couple parts, and this version of the script only works with Linux (although it can be made to work with Windows fairly easily.)

The first part is to create the script that actually checks for a file:
I created a file called /etc/checkmusic.sh
Code: Select all
#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
    echo "File '$FILE' Exists"
else
    mail -s "Unable to find Media Files" myemail@domain.com </dev/null
fi

This checks for a file specified in the next part of the script, and if the file is not found, will send an email to "myemail@domain.com". The "</dev/null" part tells your system that the body of the message is blank. (Alternatively you can redirect another text file with actual text, but I don't need that for my setup.)

Part 2 of my script is to create the file to check for and place it somewhere in your media collection. My folder format is basically:
/media/music
/media/movies
/media/audiobooks
so I put a 0-byte text file under /media and I check for that.

The third part of the script is to check every 30 mins (or however often you want).
Run "crontab -e" as root to open the cron file.
enter "0,30 * * * * /etc/checkmusic.sh /media/filename.txt" (without quotes) and then save the file.
This will run the script at the top and bottom of every hour (ie. x:00 and x:30). You can specify more or less often as needed.

Be sure your system can already send email!!! You can test this by running the "mail -s" line from the script file from the command line. If you receive an email, you're good to go. If not, you will need to setup Sendmail or another application to send mail.


In the future, I may include a body to the email message with instructions on what to check for, or how to get Madsonic to be able to access my media files again. In this instance, my server (host machine) had rebooted and the NFS exports never got exported again because the NFS service wasn't configured to start at boot. This has been fixed, so I don't see issues in the future, but I have this precaution just in case....