Question

Ubuntu server. Need cron help

  • Posted on July 15, 2014
  • dirk2099Asked by dirk2099

I have been trying to create a script to check the number of files in a directory and if the quantity of files exceeds the allowable amount the script will email the details to me. I have created a script that works fine when I execute it from terminal. If I let cron execute the script I will not receive the email of results when executed by cron. I can see the script being executed in the syslog by cron with no errors but I do not receive the email. I am new to writing scripts for Linux so please bare with me. The script checks to make sure there is no backscatter going on in email queue. The email usage is very low so 20 -30 emails an hour is a lot.

any help would be appreciated.

#!bin/sh
# checks if there are more then 40 emails in the spool and emails me if there are
count=$(ls -1 /var/spool/postfix/active | wc -l)
if [ $count -gt 40 ]
then
	SPOOL=/var/spool/postfix/; for dir in $(ls $SPOOL);do echo "$dir: $(find $SPOOL/$dir -type f |wc -l)"; done >> /tmp/spamspool.txt
	mail -s "WARNING: SPAM ATTACK in SPOOL" my-email@example.com < /tmp/spamspool.txt
	rm /tmp/spamspool.txt
fi


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Works perfectly now!

Thank you very much!

After trying to copy your script and setup a test cron job, I got a bad interpreter error.

Your script can only be run on the root directory.

Change the hashbang from #!bin/sh to #!/bin/sh

I tried to run the crontab as sudo crontab -e and still no luck.