LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to write cron job in unix system (https://www.linuxquestions.org/questions/linux-software-2/how-to-write-cron-job-in-unix-system-735836/)

ykumarreddy 06-26-2009 10:11 AM

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

colucix 06-26-2009 10:28 AM

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.

nuwen52 06-26-2009 10:29 AM

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.


All times are GMT -5. The time now is 11:00 PM.