LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-26-2009, 10:11 AM   #1
ykumarreddy
LQ Newbie
 
Registered: Jun 2009
Posts: 1

Rep: Reputation: 0
How to write cron job in unix system


Hi,

I am new to unix/linux. I don't have any knwlodge.
I need to write a cron job that has to search in log file for error. If error found I need sent an email with attachment. Can some please help to write the cron job.

Thanks in Advance.

My requirement is :

1. cd /apps/
2. grep 'emc_link' kmLogFile.log* -rn --after-context=1 --before-context=1 > ~/docAlert_todayXXXXXX.txt
3. if grep 'sendUserAlertEmail' ~/docAlert_todayXXXXXX.txt returns nothing, then send email to says everything is fine
4. if it returns something, then do the following:
grep ERROR ~/docAlert_todayXXXXXX.txt > ~/alertUser_todayXXXX.txt
grep 'emc_link' /kmLogFile.log* -rn --after-context=50 --before-context=5 > ~/alertDetail_todayXXXXXX.txt
email alertUser_todayXXXX.txt file, and message body indicates alert failure

Regards,
Kumar
 
Old 06-26-2009, 10:28 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have already done a great part of the job. Just refine the code, for example:
Code:
#!/bin/bash
today=$(date +%Y%m%d)
cd /apps/
grep 'emc_link' /path/to/kmLogFile.log* -rn --after-context=1 --before-context=1 > $HOME/docAlert_today_${today}.txt
if grep -q 'sendUserAlertEmail' $HOME/docAlert_today_${today}.txt
then
  grep ERROR $HOME/docAlert_today_${today}.txt > $HOME/alertUser_today_${today}.txt
  grep 'emc_link' /path/to/kmLogFile.log* -rn --after-context=50 --before-context=5 > $HOME/alertDetail_today_${today}.txt
  /usr/sbin/sendmail "recipient@domain" << EOF
  From: Mario Rossi <mario.rossi@domain.it>
  To: Luigi Bianchi <luigi.bianchi@domain.it>
  Subject: Alert notification
  
  $(date "+%A %e %b %Y %T") - mayday mayday mayday

  $(cat $HOME/alertUser_today_${today}.txt)

EOF
else
  /usr/sbin/sendmail "recipient@domain" << EOF
  From: Mario Rossi <mario.rossi@domain.it>
  To: Luigi Bianchi <luigi.bianchi@domain.it>
  Subject: No alert today
  
  $(date "+%A %e %b %Y %T") - everything is fine

EOF
fi
As you can see, I retrieved the current date at the beginning of the script and used the variable $today to build the file names. Then I used sendmail to send e-mail notification. Check the full path of the sendmail command (it may be different from that I used in my example). After this make the script executable using
Code:
chmod u+x name_of_the_script.sh
do some tests directly from the command line to see if it works as you expect and finally edit the crontab using
Code:
crontab -e
check the manual page of the crontab format (man 5 crontab) and feel free to ask if in doubt.
 
Old 06-26-2009, 10:29 AM   #3
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
edit: colucix has a better overall response...

A cron job is simply any executable file/program added as a cron entry. Do "man cron" and "man crontab" for more information on how to add the entry in your specific distro.

In this case, nothing here looks like you would need more than basic shell scripting. To see if grep found something, check it's return code. In shell script, $? is the variable that holds the return value of the last executed command.
Code:
example:
grep "fish" /tmp/*
RET=$?
For the e-mail part, you need a command-line e-mail program. mailx is good for that. If you need to send through remote smtp (with passwords and stuff), there's a program called sendEmail which I've used before that is also good. There are a lot of script examples out there. Search this forum for shell scripting.

Also, you should probably list the distro you are using. That might help people give you more precise advice.

Last edited by nuwen52; 06-26-2009 at 10:32 AM. Reason: leaving only because of a couple other suggestions might be useful.
 
  


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
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
Cron Job help...how to write this command? pxumsgdxpcvjm Linux - Newbie 2 09-05-2006 04:42 AM
cron job runs but will not write to logfile c_curtis Linux - General 10 06-30-2006 09:25 AM
How to write a script to run bitdefender antivirus as a cron job? ginda Linux - Software 1 03-10-2005 08:08 PM
How to Write Own Shell in Unix using System Calls? indian Programming 2 08-05-2004 04:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:03 PM.

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
Open Source Consulting | Domain Registration