Linux 从后台进程重定向标准输出和标准错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4298169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirecting stdout & stderr from background process
提问by frankscup
I have a script called foo that runs the program a.exe and sends the timing statistics to a file, time.log
我有一个名为 foo 的脚本,它运行程序 a.exe 并将计时统计信息发送到文件 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)
如果我在终端的后台运行脚本并保持 shell 打开直到 a.exe 完成,这将起作用,但是如果我在后台运行脚本并退出我的终端(a.exe 需要很长时间才能运行)
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?
当我回来时,a.exe 已执行,但时间统计信息没有出现在我的日志文件中。有人知道为什么是这样吗?有没有办法在我关闭父 shell 后获取计时统计信息?
thanks
谢谢
回答by John Kugelman
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.
当您退出 shell 时,它会向所有子进程发送一个 SIGHUP 信号,默认情况下会杀死它们。如果您希望进程在父 shell 退出时继续执行,那么您需要让它忽略 SIGHUP。
NAME
nohup -- invoke a command immune to hangups
SYNOPSIS
nohup utility [arg ...]
DESCRIPTION
The nohuputility 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.
姓名
nohup -- 调用不受挂断影响的命令
概要
nohup utility [arg ...]
描述
所述的nohup实用程序调用与它的参数,并在此时间的命令集被忽略信号SIGHUP。如果标准输出是终端,则将标准输出附加到当前目录中的文件 nohup.out 中。如果标准错误是一个终端,它被定向到与标准输出相同的地方。
回答by khachik
Try:
尝试:
nohup <your_command> &
回答by Rahly
Don't forget to remove all references to the tty/pts, 0</dev/null removes the stdin reference.
不要忘记删除对 tty/pts 的所有引用,0</dev/null 删除 stdin 引用。
回答by dennycrane
Since the question is tagged as bash
as well, I quote from man bash
由于这个问题也被标记了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.
This comes in handy when you started a job but forgot to prefix it with nohup
. Just do
这在您开始工作但忘记添加前缀时会派上用场nohup
。做就是了
disown -ah
disown -a
回答by harithski
Your a.exe gets killed when you close the parent shell.
当您关闭父 shell 时,您的 a.exe 会被杀死。
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.
您可以输入 screen,像往常一样运行命令并退出屏幕。这可以防止进程在父 shell 退出时被杀死。这种方法有点麻烦,但在其他情况下可能很方便。
John gave a much better answer - use nohup.
约翰给出了一个更好的答案 - 使用 nohup。