重定向 Bash 子脚本的输出

发布于 2024-12-22 20:10:55 字数 927 浏览 1 评论 0原文

我有一个输出各种状态消息的基本脚本。例如,

~$ ./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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏辞 2024-12-29 20:10:55

我刚刚尝试了以下操作: 我有三个文件 t1.sh、t2.sh 和 t3.sh,均包含以下内容:

#!/bin/bash

for((i=0;i<10;i++)) ; do
    echo $i of 9
    sleep 1
done

和一个名为 myscript.sh 的脚本,包含以下内容:

#!/bin/bash

./t1.sh
./t2.sh
./t3.sh

echo "All Done"

当我运行 ./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:

#!/bin/bash

for((i=0;i<10;i++)) ; do
    echo $i of 9
    sleep 1
done

And a script called myscript.sh with the following content:

#!/bin/bash

./t1.sh
./t2.sh
./t3.sh

echo "All Done"

When I run ./myscript.sh > topscript.log 2>&1 and then in another terminal run tail -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?

牵你手 2024-12-29 20:10:55

unbuffer topscript.sh >/var/log/topscript.log 2>&1

注意,unbuffer 在旧式 Unix 平台中并不总是作为 std 二进制文件提供,并且可能需要搜索并安装支持它的软件包。

我希望这有帮助。

try

unbuffer topscript.sh >/var/log/topscript.log 2>&1

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文