大网数据广告活动
服务器运维问答

Tee command is reducing the lines?

· 52CE 我爱测

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/ita…

问题原文

原帖链接 · Private · CC BY-SA 3.0

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' [email omitted]; 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' [email omitted]; fi) .

I dont know why this is happening?

THANKS

已采纳解答

原帖链接 · Paul Haldane · CC BY-SA 3.0

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' [email omitted] < /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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。