Keiji
Keiji

Reputation: 17

UNIX script to send an alert mail if file size of a flat file is zero or otherwise

I'm looking for ways to have an automated process that checks the size of a flat file in a particular location if it is zero or otherwise. Currently, I am doing it manually by going to the location and to check it. This is tedious work since I'm doing this from time to time. Could someone guide me from here? It could be a shell script in a server that when I run, it will send an email to myself and others.

Hoping for your kind response. All I see here are batch files which is not applicable to me.

sample:

location

server_name/folder1/folder2/folder3

file: test_file.dat 0 bytes

Upvotes: 1

Views: 2674

Answers (1)

user2182349
user2182349

Reputation: 9782

You could use something like this:

#!/bin/bash
# Test file with full path
if [ ! -s /tmp/test.dat ]; then
    mail '[email protected]' -s 'test.dat is empty' < /dev/null
fi;

Create a cron job using crontab -e that will check every five minutes

*/5 * * * * /home/user/check.sh

Be sure to set the execute bit on /home/user/check.sh with

chmod +x /home/user/check.sh

Notes:

  • The email content is the subject, there is no other text in the message because there is enough space to communicate the information on the subject line
  • You may need to use a program other than mail, depending on your OS, and you might have to update the command line parameters as well

Upvotes: 2

Related Questions