重定向标准输出和来自后台进程的 stderr
我有一个名为 foo 的脚本,它运行程序 a.exe 并将计时统计信息发送到文件 time.log
#!/bin/bash
date 1>> time.log
(time ./a.exe) 2>> time.log
如果我在终端后台运行该脚本并保持 shell 打开直到 a.exe 完成,那么这是有效的,但是如果我在后台运行脚本并退出终端(a.exe 需要很长时间才能运行),
foo &
exit
当我回来时,a.exe 已执行,但时间统计信息不会出现在我的日志文件中。有谁知道这是为什么?有没有办法在关闭父 shell 后获取计时统计信息?
谢谢
I have a script called foo that runs the program a.exe and sends the timing statistics to a file, time.log
#!/bin/bash
date 1>> time.log
(time ./a.exe) 2>> time.log
This works if I run the script in the background of my terminal and keep my shell open until a.exe finishes, but if I run the script in the background and exit my terminal (a.exe takes a long time to run)
foo &
exit
when I come back, a.exe has executed but the time statistics do not appear in my log file. Does anyone know why this is? Is there a way to get the timing statistics after I've closed the parent shell?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您退出 shell 时,它会向所有子进程发送 SIGHUP 信号,默认情况下会杀死它们。如果您希望进程在父 shell 退出时继续执行,那么您需要让它忽略 SIGHUP。
When you exit your shell it sends a SIGHUP signal to all child processes, which by default kills them. If you want a process to continue executing even when the parent shell exits then you need to have it ignore the SIGHUP.
由于问题也被标记为
bash
,所以我引用了man bash
当您开始一项工作但忘记在其前面加上
nohup
时,这会派上用场。代码>.只要做Since the question is tagged as
bash
as well, I quote fromman bash
This comes in handy when you started a job but forgot to prefix it with
nohup
. Just do不要忘记删除对 tty/pts 的所有引用,0
Don't forget to remove all references to the tty/pts, 0</dev/null removes the stdin reference.
当您关闭父 shell 时,您的 a.exe 会被杀死。
您可以输入 screen,照常运行命令并退出屏幕。这可以防止进程在父 shell 退出时被终止。这种方法有点麻烦,但在其他情况下可能很方便。
约翰给出了一个更好的答案——使用 nohup。
Your a.exe gets killed when you close the parent shell.
You could type screen, run the command as usual and exit the screen. This keeps the process from getting killed when the parent shell is exits. This method is a little cumbersome but might be handy in other situations.
John gave a much better answer - use nohup.
尝试:
Try: