重定向 Bash 子脚本的输出
我有一个输出各种状态消息的基本脚本。例如,
~$ ./myscript.sh
0 of 100
1 of 100
2 of 100
...
我想将其包装在父脚本中,以便运行一系列子脚本并在总体完成后发送电子邮件,例如 topscript.sh
#!/bin/bash
START=$(date +%s)
/usr/local/bin/myscript.sh
/usr/local/bin/otherscript.sh
/usr/local/bin/anotherscript.sh
RET=$?
END=$(date +%s)
echo -e "Subject:Task Complete\nBegan on $START and finished at $END and exited with status $RET.\n" | sendmail -v [email protected]
我运行如下:
~$ topscript.sh >/var/log/topscript.log 2>&1
但是,当我运行 tail - f /var/log/topscript.log
检查日志我什么也没看到,即使运行 top 显示 myscript.sh 当前正在执行,因此可能输出状态消息。
为什么子脚本中的 stdout/stderr 没有被捕获到父级日志中?我该如何解决这个问题?
编辑:我还在远程计算机上运行它们,使用伪 tty 分配通过 ssh 连接,例如 ssh -t user@host 。伪终端会干扰吗?
I have a basic script that outputs various status messages. e.g.
~$ ./myscript.sh
0 of 100
1 of 100
2 of 100
...
I wanted to wrap this in a parent script, in order to run a sequence of child-scripts and send an email upon overall completion, e.g. topscript.sh
#!/bin/bash
START=$(date +%s)
/usr/local/bin/myscript.sh
/usr/local/bin/otherscript.sh
/usr/local/bin/anotherscript.sh
RET=$?
END=$(date +%s)
echo -e "Subject:Task Complete\nBegan on $START and finished at $END and exited with status $RET.\n" | sendmail -v [email protected]
I'm running this like:
~$ topscript.sh >/var/log/topscript.log 2>&1
However, when I run tail -f /var/log/topscript.log
to inspect the log I see nothing, even though running top shows myscript.sh is currently being executed, and therefore, presumably outputting status messages.
Why isn't the stdout/stderr from the child scripts being captured in the parent's log? How do I fix this?
EDIT: I'm also running these on a remote machine, connected via ssh using pseudo-tty allocation, e.g. ssh -t user@host
. Could the pseudo-tty be interfering?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚尝试了以下操作: 我有三个文件 t1.sh、t2.sh 和 t3.sh,均包含以下内容:
和一个名为 myscript.sh 的脚本,包含以下内容:
当我运行
./myscript .sh> topscript.log 2>&1
然后在另一个终端中运行tail -f topscript.log
我看到日志文件中的行输出得很好。也许在你的下标中运行的东西使用了一个大的输出缓冲区?我知道当我之前运行 python 脚本时,它有一个相当大的输出缓冲区,所以你有一段时间看不到任何输出。您实际上看到了 topscript.sh 末尾发送的电子邮件中的完整输出吗?难道只是在进程运行时您看不到输出吗?
I just tried your the following: I have three files t1.sh, t2.sh, and t3.sh all with the following content:
And a script called myscript.sh with the following content:
When I run
./myscript.sh > topscript.log 2>&1
and then in another terminal runtail -f topscript.log
I see the lines being output just fine in the log file.Perhaps the things being run in your subscripts use a large output buffer? I know when I've run python scripts before, it has a pretty big output buffer so you don't see any output for a while. Do you actually see the entire output in the email that gets sent out at the end of topscript.sh? Is it just that while the processes run you're not seeing the output?
请
注意,unbuffer 在旧式 Unix 平台中并不总是作为 std 二进制文件提供,并且可能需要搜索并安装支持它的软件包。
我希望这有帮助。
try
Note that unbuffer is not always available as a std binary in old-style Unix platforms and may require a search and installation for a package to support it.
I hope this helps.