2

I have the following command below as part of a cronjob. When I run the command via cron or at the command line, I get Null message body; hope that's ok.

The contents of the email are empty but when I check the /tmp/sdplogs.out file it has content in it.

The script /dproxy/scripts/cleanSDP2xLogs.sh sends output to the file /tmp/sdplogs.out.

What am I doing wrong? Any help would be greatly appreciated.

$ /dproxy/scripts/cleanSDP2xLogs.sh | mail -s 'SDP2.X Prod LogArchiving Report' joeblow@xyz.com < /tmp/sdplogs.out
Null message body; hope that's ok
  • what is the content of the cleanSDP2xLogs.sh script? – Braiam Oct 15 '14 at 15:58
  • LOGDIR=/dproxy/logs/was8 find $LOGDIR/scs -name 'app.log' -mtime +30 | xargs rm -v > /tmp/sdplogs.out 2>&1 find $LOGDIR/sas -name 'app.log' -mtime +30 | xargs rm -v >> /tmp/sdplogs.out 2>&1 find $LOGDIR/scs -name 'app.log' -mtime +7 | xargs gzip -v >> /tmp/sdplogs.out 2>&1 find $LOGDIR/sas -name 'app.log' -mtime +7 | xargs gzip -v >> /tmp/sdplogs.out 2>&1 – Sandip Oct 15 '14 at 17:18
  • 2
    @Sandip Put that information in the body of the question, not in a comment. – Faheem Mitha Oct 15 '14 at 17:20
8

You need to either replace the | with && or let the script output the data to stdout and use |.

When using &&, the mail command would only be run when the script exits with 0.

When using the pipe character, your script needs to send the data to stdout so the pipe can pass it on to mail.

In what you wrote, the script writes data to the file and mail reads from the file in parallel — and mail ends up reading the file before the script has written anything to it.

|improve this answer|||||
0
/dproxy/scripts/cleanSDP2xLogs.sh | mail -s 'SDP2.X Prod LogArchiving Report' joeblow@xyz.com < /tmp/sdplogs.out

You are piping output of cleanSDP2xLogs.sh to the mail command, which I don't think is what you mean. It seems you want to first run cleanSDP2xLogs.sh to generate /tmp/sdplogs.out and then feed /tmp/sdplogs.out into the mail command.

If that's the case, just replace | with ; and you should be fine:

/dproxy/scripts/cleanSDP2xLogs.sh; mail -s 'SDP2.X Prod LogArchiving Report' joeblow@xyz.com < /tmp/sdplogs.out
|improve this answer|||||
  • That seems to be what the existing answer from over three years ago points to. – dhag Feb 21 '18 at 17:20
-1

Another way to do this use -a as attachment:

/dproxy/scripts/cleanSDP2xLogs.sh
mail -s 'SDP2.X Prod LogArchiving Report' -a /tmp/sdplogs.out joeblow@xyz.com << ""
|improve this answer|||||
  • How do you that the user's mail has an -a option? What does the << "" do at the end (it looks like the start of a here-document), did you mean <<<"" (a here-string)? – Kusalananda Apr 18 '19 at 21:07

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.