LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-13-2010, 06:33 PM   #1
LinuxNeo
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Rep: Reputation: 0
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
 
Old 10-13-2010, 07:12 PM   #2
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
What's that [filename]?
 
Old 10-13-2010, 08:38 PM   #3
LinuxNeo
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
I meant to say the current directory.
 
Old 10-13-2010, 08:54 PM   #4
LinuxNeo
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
My problem is the area where it says... when it exceeds 10 files send an email. The files in the directory will fluctuate.
 
Old 10-13-2010, 08:59 PM   #5
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
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.
 
Old 10-13-2010, 09:21 PM   #6
LinuxNeo
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
Old 10-13-2010, 11:54 PM   #7
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
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/

Last edited by Meson; 10-13-2010 at 11:59 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cronjob..... milindpk Linux - Newbie 6 05-06-2010 07:52 PM
cronjob dnaqvi Linux - General 5 04-26-2010 05:05 AM
cronjob help meeh Linux - General 1 03-26-2004 12:41 PM
Cronjob engnet Linux - Networking 6 03-03-2004 10:21 AM
Su in a cronjob michedlp Programming 3 11-06-2003 09:30 AM


All times are GMT -5. The time now is 06:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration