重定向标准输出和来自后台进程的 stderr

发布于 2024-10-04 15:15:30 字数 376 浏览 9 评论 0原文

我有一个名为 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 技术交流群。

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

发布评论

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

评论(5

白云不回头 2024-10-11 15:15:31
nohup foo &

当您退出 shell 时,它会向所有子进程发送 SIGHUP 信号,默认情况下会杀死它们。如果您希望进程在父 shell 退出时继续执行,那么您需要让它忽略 SIGHUP。

姓名

nohup -- 调用不受挂起影响的命令

概要

nohup 实用程序 [arg ...]

描述

nohup 实用程序使用其参数调用命令,此时将信号 SIGHUP 设置为忽略。如果标准输出是终端,则标准输出将附加到当前目录中的 nohup.out 文件中。如果标准错误是终端,则它会定向到与标准输出相同的位置。

nohup foo &

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.

NAME

nohup -- invoke a command immune to hangups

SYNOPSIS

nohup utility [arg ...]

DESCRIPTION

The nohup utility invokes command with its arguments and at this time sets the signal SIGHUP to be ignored. If the standard output is a terminal, the standard output is appended to the file nohup.out in the current directory. If standard error is a terminal, it is directed to the same place as the standard output.

波浪屿的海角声 2024-10-11 15:15:31

由于问题也被标记为 bash ,所以我引用了 man bash

disown [-ar] [-h] [jobspec ...]
          Without  options,  each  jobspec  is  removed  from the table of
          active jobs.  If jobspec is not present, and neither -a  nor  -r
          is  supplied, the shell's notion of the current job is used.  If
          the -h option is given, each jobspec is not removed from the ta‐
          ble,  but is marked so that SIGHUP is not sent to the job if the
          shell receives a SIGHUP.  If no jobspec is present, and  neither
          the  -a  nor the -r option is supplied, the current job is used.
          If no jobspec is supplied, the -a option means to remove or mark
          all  jobs;  the  -r  option without a jobspec argument restricts
          operation to running jobs.  The return value is 0 unless a  job‐
          spec does not specify a valid job.

当您开始一项工作但忘记在其前面加上 nohup 时,这会派上用场。代码>.只要做

disown -ah
disown -a

Since the question is tagged as bash as well, I quote from man bash

disown [-ar] [-h] [jobspec ...]
          Without  options,  each  jobspec  is  removed  from the table of
          active jobs.  If jobspec is not present, and neither -a  nor  -r
          is  supplied, the shell's notion of the current job is used.  If
          the -h option is given, each jobspec is not removed from the ta‐
          ble,  but is marked so that SIGHUP is not sent to the job if the
          shell receives a SIGHUP.  If no jobspec is present, and  neither
          the  -a  nor the -r option is supplied, the current job is used.
          If no jobspec is supplied, the -a option means to remove or mark
          all  jobs;  the  -r  option without a jobspec argument restricts
          operation to running jobs.  The return value is 0 unless a  job‐
          spec does not specify a valid job.

This comes in handy when you started a job but forgot to prefix it with nohup. Just do

disown -ah
disown -a
無處可尋 2024-10-11 15:15:31

不要忘记删除对 tty/pts 的所有引用,0

Don't forget to remove all references to the tty/pts, 0</dev/null removes the stdin reference.

妞丶爷亲个 2024-10-11 15:15:31

当您关闭父 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.

本王不退位尔等都是臣 2024-10-11 15:15:31

尝试:

nohup <your_command> &

Try:

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