gpt4 book ai didi

linux - 从后台进程重定向标准输出和标准错误

转载 作者:IT王子 更新时间:2023-10-29 01:06:43 26 4
gpt4 key购买 nike

我有一个名为 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 后获取计时统计信息?

谢谢

最佳答案

nohup foo &

当您退出 shell 时,它会向所有子进程发送一个 SIGHUP 信号,这在默认情况下会杀死它们。如果您希望进程在父 shell 退出时继续执行,那么您需要让它忽略 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.

关于linux - 从后台进程重定向标准输出和标准错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4298169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com