![](/img/trans.png)
[英]Redirecting stderr, stdout and stdin to pipe is blocking the process from running
[英]Redirecting stdout & stderr from background process
我有一个名为foo的脚本,该脚本运行程序a.exe并将计时统计信息发送到文件time.log。
#!/bin/bash
date 1>> time.log
(time ./a.exe) 2>> time.log
如果我在终端的后台运行脚本并保持外壳打开直到a.exe完成,则此方法有效,但是如果我在后台运行脚本并退出终端(a.exe需要很长时间才能运行)
foo &
exit
当我回来时,已经执行了a.exe,但是时间统计信息没有出现在我的日志文件中。 有人知道为什么吗? 关闭父外壳后,是否可以获取时序统计信息?
谢谢
nohup foo &
退出外壳程序时,它将向所有子进程发送SIGHUP信号,默认情况下会杀死它们。 如果要使进程即使在父外壳退出时也继续执行,则需要让它忽略SIGHUP。
名称
nohup-调用不受挂断的命令
概要
nohup utility [arg ...]
描述
nohup实用程序使用其参数调用命令,这时将信号SIGHUP设置为忽略。 如果标准输出是终端,则将标准输出附加到当前目录中的文件nohup.out。 如果标准错误是端子,则将其定向到与标准输出相同的位置。
由于该问题也被标记为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
尝试:
nohup <your_command> &
不要忘记删除所有对tty / pts的引用,0 </ dev / null会删除标准输入。
关闭父外壳程序时,您的a.exe被杀死。
您可以键入screen,像往常一样运行命令并退出屏幕。 这样可以避免退出父shell时杀死进程。 这种方法有点麻烦,但在其他情况下可能会很方便。
约翰给出了一个更好的答案-使用nohup。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.