2

I have the code like this :

54 08 * * * /usr/local/bin/curator --dry-run --config /home/itadmin/.curator/curator.yml /home/itadmin/.curator/daily.yml 2>&1 | /usr/bin/tee -a /home/itadmin/.curator/logs.txt | /usr/bin/tee /home/itadmin/.curator/history.txt | if [ $(wc -l </home/itadmin/.curator/history.txt) -ge 2 ]; then  mail -s 'Snapshot Status' xyz@abc.com; fi

What i am doing is i am sending the output of cron to logs.txt for history purpose and sending the same to history.txt in which the if condition will work.

Actually the cron output is like this

2017-05-17 08:33:01,395 INFO      Preparing Action ID: 1, "snapshot"
2017-05-17 08:33:01,404 INFO      Master-only flag detected. Connected to non-master node. Aborting.

But i got in logs.txt is

2017-05-17 08:54:01,427 INFO      Preparing Action ID: 1, "snapshot"

IN history.txt file i got nothing

But these worked fine when i did the remove the if command(i.e.if [ $(wc -l </home/itadmin/.curator/history.txt) -ge 2 ]; then mail -s 'Snapshot Status' xyz@abc.com; fi) .

I dont know why this is happening?

THANKS

Private
  • 277
  • 1
  • 2
  • 9
  • Could you try `| tee file1 file2` instead of `| tee | tee`? – Xavier Nicollet May 17 '17 at 09:43
  • Normally tee command will overwrite the file so i need to append to a file and same way overwrite to another like this `| tee -a logs.txt history.txt `(i.e.it means it append cron output in logs.txt and overwrite the output in history.txt file. When i used like this `| tee -a logs.txt history.txt ` it is not working – Private May 17 '17 at 09:56

1 Answers1

1

You need to split your job into two commands (as below) so that history.txt is closed before you start checking its size.

What's happening is that the if statement looks at the size of the file after it has been truncated but before it has been written to. That means that the last component of the pipeline just exits and everything before that dies with SIGPIPE.

54 08 * * * /usr/local/bin/curator --dry-run --config /home/itadmin/.curator/curator.yml /home/itadmin/.curator/daily.yml 2>&1 | /usr/bin/tee -a /home/itadmin/.curator/logs.txt > /home/itadmin/.curator/history.txt; if [ $(wc -l </home/itadmin/.curator/history.txt) -ge 2 ]; then mail -s 'Snapshot Status' xyz@abc.com < /home/itadmin/.curator/history.txt; fi

I'm not aware of a way of doing this without using the intermediate file. An answer to Sending mail from command line if body not empty suggests use of the ifne utility for the special case of empty/non-empty input.

Paul Haldane
  • 4,457
  • 1
  • 20
  • 31