我有一个名为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
06001
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.