Operating System - HP-UX
1823988 Members
4083 Online
109667 Solutions
New Discussion юеВ

Any way to change subject on cron email ?

 
SOLVED
Go to solution
S.Rider
Regular Advisor

Any way to change subject on cron email ?

I'd like to change the subject from "cron" to something signifying the actual job run on the hundreds of emails I get from my servers daily. I'm guessing the answer is "no", but does anyone know of a way to get a usefull header on cron job emails ?
Ride Boldly Ride, but watch out for El Dorado's
12 REPLIES 12
Steven E. Protter
Exalted Contributor
Solution

Re: Any way to change subject on cron email ?

The answer is YES.


/usr/contrib/bin/script 2>&1 mailx -s "subject here" email@your.net

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Patrick Wallek
Honored Contributor

Re: Any way to change subject on cron email ?

I think SEP missed the pipe symbol in his resonse.

1 2 3 4 5 /some/dir/script_to_run 2>&1 | mailx -s "/some/dir/script_to_run cron output" your_email@somewhere.com
Kent Ostby
Honored Contributor

Re: Any way to change subject on cron email ?

Wouldn't that be a ";" instead of a pipe ?

1 2 3 4 5 /some/dir/script_to_run 2>&1 ; mailx -s "/some/dir/script_to_run cron output" your_email@somewhere.com
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Patrick Wallek
Honored Contributor

Re: Any way to change subject on cron email ?

Ummmm.....No.

The pipe will pipe the output of the script, both stdout and stderr with the 2>&1 redirect, to mailx.

The semi-colon will just run a mailx command after the cron job runs. It would likely just sit and wait for input since you aren't sending anything to the command. And you would still get the e-mail with the "cron" subject.

Try it and see what happens.
Senthil Kumar .A_1
Honored Contributor

Re: Any way to change subject on cron email ?

Hi Patrick,

This was your example....

1 2 3 4 5 /some/dir/script_to_run 2>&1 | mailx -s "/some/dir/script_to_run cron output" your_email@somewhere.com

I do agree there should be a pipeline which SEP missed... but i think "2>&1" is not necessary when i use a pipeline to direct to another program...because "2>&1" i had use it if i wanna send the output and error to a single file, but here we are not directing to a file but to standard output which inturn is fed to mailx program through pipeline. so i think the following should just work fine...

1 2 3 4 5 /some/dir/script_to_run | mailx -s "/some/dir/script_to_run cron output" your_email@somewhere.com
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Patrick Wallek
Honored Contributor

Re: Any way to change subject on cron email ?

I disagree.

When you run a cronjob you normally DO want both STDOUT and STDERR.

If you just do:

command | mailx

You will ONLY get STDOUT.

Think of it as if you were running a command from the prompt.

Take this test for example:

# ls a* b* c* | mailx -s test you@somewhere.com

If there are no files present you will see the errors on the screen and you will get a blank e-mail.

Now try:

# ls a* b* c* 2>&1 | mailx -s test you@somewhere.com

With this you will see NO output on the screen, but you will get an e-mail with the errors in it.

The same concept holds true for cron.

If you were to do a cron job like my first example above you would get 2 e-mails. 1 e-mail would have the subject test and would be empty (assuming no files of the name a* b* c* in the directory) or would contain the list of files and the other would have the cron subject and contain the errors.

Now if you were to do my 2nd example within cron (with the 2>&1), then you would get a single e-mail with the command output and the errors, if any.

If you don't believe me, do some testing on your own.
Thom Cornwell
Frequent Advisor

Re: Any way to change subject on cron email ?

So now would there would be two email notifications? One from Cron, and the other from the output being directed to mailx?
Thom Cornwell
Frequent Advisor

Re: Any way to change subject on cron email ?

Never mind the last question, but while I was researching, I came across MAILTO variable in LINUX. Does HPUX support MAILTO variable in crontables?
Greg Vaidman
Respected Contributor

Re: Any way to change subject on cron email ?

Thom,
There would NOT be two email notifications. Since all the output is being redirected into the pipe and sent to mailx, cron does not generate an email at all.
--Greg
Senthil Kumar .A_1
Honored Contributor

Re: Any way to change subject on cron email ?

Hi patrick,

sorry for overlooking this,, as i din't give a thought in the file discriptor level. When i give a second thought based on your reply, i find it logical.

Amateur that i'm , i will try this in the next opportune time when i chance upon a system. I admit that i din't use the "combined redirection" just before a pipeline and would certainly add it to the list of goodies..after a performing the test you suggested..

:)
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
S.Rider
Regular Advisor

Re: Any way to change subject on cron email ?

SEP & Patrick's reply of
1 2 3 4 5 /some/dir/script_to_run 2>&1 | mailx -s "/some/dir/script_to_run cron output" your_email@somewhere.com
worked great for me. Thanks.
Ride Boldly Ride, but watch out for El Dorado's
Bill Hassell
Honored Contributor

Re: Any way to change subject on cron email ?

Just to clarify: cron never sends email UNLESS there is something sent to stdout. So if the command/script has stdout redirected then cron never sees any output and thus no email (from cron)


Bill Hassell, sysadmin