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 |
You have already done a great part of the job. Just refine the code, for example:
Code:
#!/bin/bash Code:
chmod u+x name_of_the_script.sh Code:
crontab -e |
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: 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. |