pickle
pickle

Reputation: 1045

Crontab, want to mail just errors to myself AND log output in separate file

I am very new to crontab, I've only just started using it today.

What I'm trying to do is fairly straightforward (I think.) I want all errors that may result from my script to be mailed to myself and all output (whether it is successful or not) to be logged in a separate log file.

Currently, it's logging the output just fine but I'm not sure where to begin to get it to email me for just errors.

This is the code I have thus far:

[email protected]

# m h dom mon dow   command

0,15,30,45 * * * * /path/to/script >> /path/to/log/$(date +\%m\%d\%H\%M)_f.log 2>&1

Thanks!

Upvotes: 2

Views: 820

Answers (3)

user6784987
user6784987

Reputation:

Or simply

* * * * * /.../script 2>&1 | tee script.log

Upvotes: 0

ftorto
ftorto

Reputation: 67

I suggest you to encapsulate your script in another one. You will add this new script to cron

encap.sh content:

#call your script
script.sh > /tmp/temp$$.log

#analyze results using /tmp/temp$$.log or $?

#send mail depending on results

#Log results
cat /tmp/temp$$.log >> /path/to/log/$(date +\%m\%d\%H\%M)_f.log
#or 
mv /tmp/temp$$.log > /path/to/log/$(date +\%m\%d\%H\%M)_f.log

Upvotes: 1

wind39
wind39

Reputation: 451

Try mailx. You need to pass a ~/.mozilla/firefox/xxxxx.default directory for it to work properly.

#! /bin/bash

MACHINE=$(hostname)
CURRENT_TIME=$(date +%Y-%m-%d_%H-%M-%S)                                                                                                                                                                                          

mailx -v -s "BACKUP FAILED" -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp://smtp.server.com:587 -S [email protected] -S [email protected] -S smtp-auth-password=namesecret -S ssl-verify=ignore -S nss-config-dir=/home/name/.mozilla/firefox/xxxxxxx.default/ [email protected] << EOF                                   
Hello,                                                                                                                                                                                                                           

This message was sent by an automatic script. Please do not reply.                                                                                                                                                               

If you are reading this, it means that the machine $MACHINE could not connect to the External Backup Server.                                                                                                                     

Please check this situation.                                                                                                                                                                                                     

Regards,

The $MACHINE Administrator.
EOF

Upvotes: 0

Related Questions