1834149 Members
3677 Online
110064 Solutions
New Discussion

Re: cron

 
navin
Super Advisor

cron

I have created a cron entry ,i would like to get the output in e-mail.please help.
Thanks
Learning ...
9 REPLIES 9
Ken Penland_1
Trusted Contributor

Re: cron

* * * * * command you are running |mailx your.address@yoursite.com
'
Sridhar Bhaskarla
Honored Contributor

Re: cron

Hi,

One approach is to create a wrapper script that will capture the log and send it to the user. It may look like

script_wrapper

#!/usr/bin/ksh
USER=yourid@youruser.com
LOG=/tmp/log$$
/your_path/your_script > $LOG 2>&1
mailx -s "Out put of your_script" $USER < $LOG


Put the above script into the cron.

One more approach is to put the mailx directly into the cront

* * * * * /your_path/Your_script |mailx -s "cronout" yourid@youruser.com

I would actually create an alias for your user in the system so that all the mails to your account will come to you.

Edit /etc/mail/aliases and point user to yourid@yourhost.com.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Bill Douglass
Esteemed Contributor

Re: cron

cron will e-mail any standard output and standard error generated by the cron job to the user id that owns that crontab.

If you want to send it to a different e-mail address, then redirect your stdout (and std err) to a mail command, like so:


/path/to/command > /usr/bin/mailx -s "Cron Output" user@somewhere.com 2>$1

Bill Douglass
Esteemed Contributor

Re: cron

Sorry, that should be

/path/to/command | /usr/bin/mailx -s "Cron Output" user@somewhere.com 2>$1

Need a pipe there.
Francisco J. Soler
Honored Contributor

Re: cron

Hi,
from man crontab:
....
Be sure to redirect the standard output and standard error from commands. If this is not done, any generated standard output or standard error is mailed to the user.
......

In linux, before any crontab entries you must put a line with the variable:
MAILTO=user

Frank.
Linux?. Yes, of course.
Dario_1
Trusted Contributor

Re: cron

Hi!

Edit your crontab using crontab -e or sam and add the following to the crontab line:

| mailx -s "Subject you want" user@domain.com 2>$1

Note that I am starting this with a pipe.

Regards,

DR
Dario_1
Trusted Contributor

Re: cron

Hi!

Edit your crontab using crontab -e or sam and add the following to the crontab line:

| mailx -s "Subject you want" user@domain.com 2>$1

Note that I am starting this with a pipe.

Regards,

DR
navin
Super Advisor

Re: cron

Thanks for all.Replies were very useful.
Thanks
Learning ...
Patrick Wallek
Honored Contributor

Re: cron

From what I can tell here, most everyone that is pipeing the script output to mailx has their stderror to stdout redirect in the wrong place.

What I am seeing:

script | mailx -s "subject" mail@junk.com 2>&1

will not work like you think. That will only redirect standard error to standard out for the mailx command, NOT the script.

What you really probably want is:

script 2>&1 | mailx -s "subject" mail@junk.com

The above will redirect the standard error to standard out for the script that is run and THEN pipe that output into the mailx command. This way you get any output and errors from your script mailed to you.