LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cronjob help (https://www.linuxquestions.org/questions/linux-newbie-8/cronjob-help-837900/)

LinuxNeo 10-13-2010 05:33 PM

cronjob help
 
Hi I am very new to linux. I want to run a cronjob every 15 minutes that checks a directory for files. If the directory contains more than ten files I want it to send an email to me.

All I have is this...

*/15 * * * * ls -l | wc -l | [filename] | mail -s "This is just a test" [email address]

I would rather not write a bash script. Is there an easier way to do this? I was looking into some commands like find and grep

Could you show me an example?

Thanks,
LN

Meson 10-13-2010 06:12 PM

What's that [filename]?

LinuxNeo 10-13-2010 07:38 PM

I meant to say the current directory.

LinuxNeo 10-13-2010 07:54 PM

My problem is the area where it says... when it exceeds 10 files send an email. The files in the directory will fluctuate.

Meson 10-13-2010 07:59 PM

I still don't know what you mean by [filename], you can't pipe to a filename. You would need something like this:

Code:

ls -1 /path/to/dir | wc -l | mail -s "subject" "name@domain.net"
However, that *still* won't work because you can't embed the logic to check for 10 into a cronjob. You have to make a script. Put it in /usr/local/bin. Make sure the script does not write to stdout, or redirect stdout when calling the script.

LinuxNeo 10-13-2010 08:21 PM

I'm so new...to this.

I don't know how to write a script for this. How do I incorporate all this to email myself when this stuff happens?

#!/bin/bash

if [ [ls -l | wc -l] > 10 ]; then
mail -s [email address];
fi


I have no way to test this, until I'm in school.

Thanks for the help.

Meson 10-13-2010 10:54 PM

You have a great way to test this: install Linux =)

Code:

#!/bin/bash

DIR=/path/to/somedir
LIST="email@address.com email2@address.com"
MAX=10

count=$(ls -1 "$DIR" | wc -l)

if [[ "$count" -gt "$MAX" ]]; then

        /usr/bin/mailx -s "More than 10 files in $DIR" $LIST
fi

I don't really know the difference between mail and mailx, but mailx is the one I tend to use.

Here's a great reference: http://www.tldp.org/LDP/abs/html/


All times are GMT -5. The time now is 09:25 PM.