为什么重定向输出不会修改目标文件的上次修改时间字段?
我有一个在 Cygwin 中运行的 bash 脚本,其输出已重定向到另一个文件。如果我将 bash 脚本作为批处理作业运行,MS-DOS 也会表现出相同的行为。
bash.exe &> log.txt
我知道 bash.exe 会定期转储输出,因此我只想监视 log.txt 的上次修改时间以确定 bash.exe 是否已挂起。
不幸的是,通过重定向向 log.txt 添加内容不会更改 log.txt 的上次修改时间。
# ls -la --full-time log.txt
-r-xr-x---+ 1 user Domain Users 66455 2011-11-30 16:16:45.246664800 -0500 log.txt
一段时间后...
# ls -la --full-time log.txt
-r-xr-x---+ 1 user Domain Users 66838 2011-11-30 16:16:45.246664800 -0500 log.txt
请注意,尽管 log.txt 变得更大,但上次修改时间并未改变。仅当 bash.exe 终止时才会更新上次修改时间。
在我看来,Ubuntu 11.04 正确地处理了这种情况。
CYGWIN:CYGWIN_NT-6.1
MS-DOS:Microsoft Windows [版本 6.1.7601]
I've got a bash script running in Cygwin whose output has been redirected to another file. MS-DOS exhibits this same behavior if I run the bash script as a batch job.
bash.exe &> log.txt
I know that bash.exe regularly dumps output so I'd like to just monitor the last modified time of log.txt to determine if bash.exe has hung.
Unfortunately, adding content to log.txt through redirection does not change log.txt's last modified time.
# ls -la --full-time log.txt
-r-xr-x---+ 1 user Domain Users 66455 2011-11-30 16:16:45.246664800 -0500 log.txt
Some time later...
# ls -la --full-time log.txt
-r-xr-x---+ 1 user Domain Users 66838 2011-11-30 16:16:45.246664800 -0500 log.txt
Note that even though log.txt has gotten larger, the last modified time has not changed. The last modified time is updated only when bash.exe terminates.
Ubuntu 11.04 handles this scenario, IMO, correctly.
Cygwin: CYGWIN_NT-6.1
MS-DOS: Microsoft Windows [Version 6.1.7601]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法仅使用 Cygwin 重现此问题,因此我无法准确地呈现问题。我的流程是在 Java 进程中调用 Runtime.getRuntime.exec() ,该进程打开 cygwin/bin/bash.exe,然后从 bash 中运行重定向命令。在调用
exec()
的同一个 Java 进程中,我尝试根据之前调用的 bash.exe 中发生的情况来监视上次修改时间。我已转而检查文件长度,以确定进程是否已挂起,这是有效的。我猜这是某种范围问题。
I'm having trouble reproducing this using just Cygwin so I'm not presenting the problem accurately. My process is to call
Runtime.getRuntime.exec()
within a Java process which opens cygwin/bin/bash.exe and then runs the redirection command from within the bash. From the same Java process that calledexec()
, I'm trying to monitor the last modified time based on what's going on in the previously called bash.exe.I have switched to checking the file length instead to determine if the process has hung, which is working. I guess it's some kind of scope problem.