Cronjob to notify by mail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cronjob to notify by mail
# 1  
Old 10-31-2010
Cronjob to notify by mail

I am planning some tasks that I need send mails regularly to my team members. I think to use cronjob with script to do it.

Two init files are prepared, I can change the format if you ask for.

Code:
name.txt:

Bill bill@xxx.com
Peter peter@xxx.com
James james@xxx.com

Code:
Tasks_list.txt

1|check windows servers' health|Peter,bill|fornightly
do the job 1
do the job 2
do the job 3 
2|check the mail servers' health|James|monthly
do the job 4
do the job 5
3|check linux servers' health|bill|weekly
do the job 6
do the job 7
do the job 8

I can change fornightly to 2, monthly to 4, if you think that will be simple for coding.

So the script will read the task_list.txt, and send mail to the nominate person regularly.

1. Can you help to build a script frame for me? i can do some updates, but don't know how to start.
Code:
for i in `cat tasks_list.txt`
do
   check and send mail to Peter,bill, if task 1 need be done, list the jobs |mailx -s "task 1 need be done " peter@xxx.com bill@xxx.com
   check and send mail to James, if task 2 need be done. list the jobs |mailx -s "task2 need be done" james@xxx.com
   etc
done

2. if some tasks need be done fornightly, or quarterly, how can I set it?
# 2  
Old 10-31-2010
Example cron config
Code:
# On the 1st of the month at 8am notify Task 2 jobs
0 8 1 * * /usr/local/bin/mynotify 2
 
# On the 1st and 15th of the month at 7:30am Task 1 jobs
30 7 1,15 * * /usr/local/bin/mynotify 1
 
# On Friday at 11:15pm notify Task 3 jobs
15 23 * 5 /usr/local/bin/mynotify 3
 
# On 1st Apr (1st Quarter) at 8am notify Task 4 jobs
0 8 1 4 * /usr/local/bin/mynotify 4

For quarterly checks you are probably best of creating 1 job for each of the 4 dates (see 1st April above).

Now you just need to write your "mynotify" script that accepts a task number and sends the emails out. It can read your config files.

Code:
$ cat tasks_list.txt
1:1,2,3:Check windows servers' health:Peter Bill
2:4,5:Check the mail server's health:James
3:6,7,8:Check linux server's health:Bill

$ cat name.txt
Bill bill@xxx.com
Peter peter@xxx.com
James james@xxx.com

$ cat mynotify 
TASK=$1
TODO=$(grep "^$TASK" tasks_list.txt)
[ -z "$TODO" ] && exit
JOBS=$(echo $TODO | cut -f2 -d:)
TITLE=$(echo $TODO | cut -f3 -d:)
WHO=$(echo $TODO | cut -f4 -d:)
echo $WHO | tr ' ' '\n' | while read USR
do
    EMAIL=$(grep "^$USR " name.txt | cut -f2 -d' ')
    echo mail to $USR \($EMAIL\)  subject $TITLE  jobs $JOBS  
done

$ ./mynotify 1
mail to Peter (peter@xxx.com) subject Check windows servers' health jobs 1,2,3
mail to Bill (bill@xxx.com) subject Check windows servers' health jobs 1,2,3


Last edited by Chubler_XL; 10-31-2010 at 11:32 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 11-01-2010
Thanks a lot. i will set the cronjob as your suggested.

But I need mail the detail job tasks (job 1, job 2,....) within mail to the person. How can I do that?
# 4  
Old 11-01-2010
Might be best to have a file for each taks with job list
eg. task_jobs_1.txt:
Code:
    - Get users to log off
    - Stop MySQL servers
    - Do system Backup

And cat the job_task file to you mailx command:

Code:
     cat task_jobs_${TASK}.txt | mailx -s "$TITLE" $EMAIL

# 5  
Old 11-01-2010
If you are wanting to send to an external mail system (i.e. not on the local host unix server) you will need to edit the configuration file /etc/sendmail.cf or /etc/sendmail/sendmail.cf or whatever depending on your OS. You are looking for the DS record which you will need to uncomment and append your mail gateway to it so you end up with a line such as:
Code:
DSmymailroute.mycompany.com

Best to use a DNS name for this assuming you have one. At worst, code an entry into /etc/hosts, but I'd rather avoid that because of the hassle to change it individually if the mail server changes IP addess.



I hope that this helps.

Robin
Blackburn/Liverpool
UK
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A notify-send Question --

Greetings. I've come across a bit of a problem with notify-send syntax; and wondered if anyone out there had found a solution in the woodpile somewhere :) Here's the snippet which is giving me trouble:notify-send 'Text to display' image_data 300, 300, , , , , /etc/image.pngOutput:Invalid... (0 Replies)
Discussion started by: LinQ
0 Replies

2. UNIX for Dummies Questions & Answers

how to cancel a cronjob if the cronjob still running

hi everyone I'm newbie in this forum hope I can get some help here :) I have a command in crontab that executed every 1 minute sometime this command need more than 1 minute to finish the problem is, the crontab execute this command although it's not finish processing yet and causing the system... (7 Replies)
Discussion started by: 2j4h
7 Replies

3. Shell Programming and Scripting

notify-send does not notify real time

Hi, I am having a little trouble getting notify-send to work the way I would like it to. I am using ubuntu - karmic koala 2.6.31-19-generic #56-Ubuntu SMP So here's the problem run the following commands one after the other. notify-send -i info -t 100000 -- "Hi" "world" & notify-send -i... (3 Replies)
Discussion started by: linuxpenguin
3 Replies

4. Shell Programming and Scripting

Using mail command to notify the status of script

Can someone please help me with this script, I'm trying to create system backup on AIX, for this I want to first mount the filesystem if it is not mounted, then create the backup and unmount the filesystem but I'm having problem while using the mail command to notify the status of filesystem... (9 Replies)
Discussion started by: mbak
9 Replies

5. UNIX for Advanced & Expert Users

Notify Me when job fail using cron

I would like to be notified when an Informix dbexport job fails on a TRU64 system. My idea is to send an email via telnet, but can only get telnet to do it interactively. I can do the following steps on my UNIX box interactively, but cannot send an email with a script. Can someone help? Is there... (3 Replies)
Discussion started by: ejaggers
3 Replies

6. Shell Programming and Scripting

Cronjob - Redirect mail output to file, LINES & COLUMNS

I currently have an expect script that issues the 'mail' command and sends an 'x' when it receives the & prompt from mail to quit. The expect script is able to do stty rows 100 columns 200 < $spawn_out(slave,name) to set up the number of columns and rows. I would like to get rid of the expect... (0 Replies)
Discussion started by: jharvey
0 Replies

7. UNIX for Dummies Questions & Answers

Notify Command.

Hi, I am using the NOTIFY command and I am getting the emails.. I tried MAILX and e-mail is being sent. I want to use NOTIFY to see if my jobs are completed or not. Am I missing something.. All your ideas are greatly appreciated.. Thanks Venky (2 Replies)
Discussion started by: venkyA
2 Replies

8. Post Here to Contact Site Administrators and Moderators

Notify of a new thread

Hi, I wish to be notified whenever there is a new thread posted on any of the forums or atleast the forums that I have registered to. Can this be done. How? I am sure there must be a way to do this, and I feel this is how the moderators must be doing. I know I may not be eligible enough to be... (5 Replies)
Discussion started by: linuxpenguin
5 Replies

9. UNIX for Advanced & Expert Users

mail from Cronjob

Hi, I'm having a HP UX 11i and 11.0 . I've given the cron access to one of the user. Now he is getting mails on errors only and not on success on execution of the command. How can i make sure that the mail is sent in both the cases of success and failure. Also the mail is going to the lotus... (6 Replies)
Discussion started by: i2admin
6 Replies

10. UNIX for Advanced & Expert Users

process pager - by cell phone or just e-mail notify

hi all.. i need any help. i want to create in my crontab a simple script for verify any process and notify for the status in my e-mail or cell phone. anybody help me? (9 Replies)
Discussion started by: squash
9 Replies
Login or Register to Ask a Question