0

I want to get mail from cron tab if output of it is greater than 100 characters.

For getting mail i had installed the postfix . But what i want to know is it above scenario is possible or not? If it is possible how the script should be?

Example:

MAILTO:zxy@example.com

5 07 * * * /usr/local/bin/curator --dry-run --config /home/itadmin/.curator/curator.yml /home/itadmin/.curator/snapshot.yml 2>&1 | /usr/bin/tee -a /home/itadmin/.curator/logs.txt 

By this i am getting email for every output coming out of the above cron job but i want to send a mail only if the crontab output is greater than 100 characters.

Thanks

  • what output should be greater than 100 ? could you explain to me ? – Mostafa Hussein May 13 '17 at 12:08
  • @hussein i updated the question – Private May 13 '17 at 12:19
  • the email you receive thats only to inform you when the cron is execute. still don't get it what do you mean by crontab output ? do you mean your script output ? as the crontab itself has output everytime under /var/log/cron – Mostafa Hussein May 13 '17 at 13:33
0

Cron's internal mailing is really limited: it either sends output of the command or not, with headers hard-coded in do_command.c:

358     /* if we are supposed to be mailing, MAILTO will
359      * be non-NULL.  only in this case should we set
360      * up the mail command and subjects and stuff...
361      */
362
363     if (mailto) {

375         fprintf(mail, "From: root (Cron Daemon)\n");
376         fprintf(mail, "To: %s\n", mailto);
377         fprintf(mail, "Subject: Cron <%s@%s> %s\n",
378             usernm, first_word(hostname, "."),
379             e->cmd);

If you need anything more than that you should save the output into a file and invoke another commands to evaluate its contents and mail it when it meets the conditions desired.

0

Use awk to count the char on the standard output. Save lines until more than 100 char where written. Print saved lines and then keep printing until the end of the output:

/usr/local/bin/curator --dry-run --config /home/itadmin/.curator/curator.yml /home/itadmin/.curator/snapshot.yml 2>&1 | awk '{ if ( morethan100 == 1 ) { print $0 ; next } } { count += length } { a[NR]=$0 } { if (count > 100) { for (i=1 ; i <= NR ; i++) { print a[i] } morethan100=1 } }'

This will produce no output (and no mail) when your command produces less than 100 char; otherwise it will output everything (and mail it) when the your command exceeds 100 chars.

If you want to count newlines as characters you need to add 1 for each line:

/usr/local/bin/curator --dry-run --config /home/itadmin/.curator/curator.yml /home/itadmin/.curator/snapshot.yml 2>&1 | awk '{ if ( morethan100 == 1 ) { print $0 ; next } } { count += length + 1 } { a[NR]=$0 } { if (count > 100) { for (i=1 ; i <= NR ; i++) { print a[i] } ; morethan100=1 } }'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.