如何在linux上创建临时文件,无论如何都会自动清理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9889277/
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
How to create temporary files on linux that will automatically clean up after themselves no matter what?
提问by thejh
I want to create a temporary file on linux while making sure that the file will disappear after my program has terminated, even if it got killed or someone performs a hard reboot in the wrong moment. Does tmpfile()
handle all this for me?
我想在 linux 上创建一个临时文件,同时确保该文件在我的程序终止后消失,即使它被杀死或有人在错误的时刻执行了硬重启。是否tmpfile()
处理所有这些给我吗?
采纳答案by glglgl
You seem pre-occupied with the idea that files might get left behind some how because of some race condition, I don't see an explanation of why this is a concern.
您似乎专注于文件可能会因为某些竞争条件而被遗忘的想法,我没有看到为什么这是一个问题的解释。
"A race condition occurs when a program doesn't work as it's supposed to because of an unexpected ordering of events that produces contention over the same resource."
“当程序由于意外的事件排序而导致对同一资源产生争用而无法按预期工作时,就会发生竞争条件。”
I was assuming that from your comments on other answers your concern was specifically on a dead-lockwhich is a result of trying to remediate a race-condition ( contention of the shared resource ). It is still not clear what your concern is, calling tmpfile()
and having the program exit abnormally before that function gets to call unlink()
is the least of your worries if your application is really that fragile.
我假设根据您对其他答案的评论,您的担忧特别是由于尝试修复竞争条件(共享资源的争用)而导致的死锁。仍然不清楚您的担忧是什么,如果您的应用程序真的那么脆弱,那么tmpfile()
在该函数被调用之前调用并让程序异常退出unlink()
是您最不担心的。
Given that there isn't any mention of concurrency, threading or other processes sharing this file descriptor to this temp file, I still don't see the possibility for a race condition, maybe the concept of an incomplete logical transaction, but that can be detected and cleaned up.
鉴于没有提到并发、线程或其他进程将这个文件描述符共享给这个临时文件,我仍然看不到竞争条件的可能性,也许是不完整逻辑事务的概念,但这可能是检测和清理。
The correct way to make absolutely sure that any allocated file system resources are cleaned up is notsolely on exitof an application but also also on start-up. All my server code, makes sure that everything is cleaned up from a previous run beforeit starts and makes itself available.
确保清除任何分配的文件系统资源的正确方法不仅是在应用程序退出时,而且在启动时也是如此。我所有的服务器代码,确保在它开始之前从上一次运行中清除所有内容并使其可用。
Put your temp files in a sub-dir in /tmp
make sure your application cleans this sub-dir on startup and normal shutdown. You can wrap your app start up with a shell script that detects abnormal ( kill -9
) shutdown based on PID
existence and also does clean up activities.
将您的临时文件放在一个子目录中,/tmp
以确保您的应用程序在启动和正常关闭时清除此子目录。您可以使用 shell 脚本来包装您的应用程序启动,该脚本kill -9
根据PID
存在检测异常 ( ) 关闭并清除活动。
回答by flolo
EDIT: Yes
编辑:是的
I checked the tmpfile source, and it does indeed use glglgl trick, and instantly unlocks the file.
我检查了 tmpfile 源,它确实使用了 glglgl 技巧,并立即解锁了文件。
Original:
原来的:
I would say no. Got killed should work, but I would assume that it can happen, that after a hard reboot (e.g. due to power outtake) the file is still there. But that depends on your Linux distribution and the used settings.
我会说不。被杀死应该可以工作,但我认为它可能发生,在硬重启(例如由于断电)后,文件仍然存在。但这取决于您的 Linux 发行版和使用的设置。
If the temp file is created in a ramdisk, it is gone (there are unix distris out there that e.g. use a ram based tmpfs for temporary files).
如果临时文件是在 ramdisk 中创建的,它就会消失(那里有 unix 发行版,例如使用基于 ram 的 tmpfs 来存储临时文件)。
Or if you use an environment that has certain policy regarding tmp, it could be also gone (maybe not instant, but often there are policies, like e.g. remove all files in /tmp that are not accessed within one month), but it could be also on a standard file system where such rules are not enforced. In this case the file would stay.
或者,如果您使用的环境对 tmp 有一定的政策,它也可能消失(可能不是即时的,但通常有政策,例如删除 /tmp 中一个月内未访问的所有文件),但它可能是也在不强制执行此类规则的标准文件系统上。在这种情况下,文件将保留。
回答by logoff
according to tmpfile() man page:
根据 tmpfile() 手册页:
The file will be automatically deleted when it is closed or the program terminates.
该文件将在关闭或程序终止时自动删除。
I have not tested, but it seems it should do what you want.
我没有测试过,但它似乎应该做你想做的。
Moreover:
而且:
The default location, if TMPDIR is not set, is /tmp.
如果未设置 TMPDIR,则默认位置为 /tmp。
Then, when a reboot is produced, /tmp
will be empty.
然后,当产生重新启动时,/tmp
将是空的。
回答by tripleee
The customary approach is to set up a signal handler to clean up if the program is interrupted. This will not handle kill -9
or a physical reboot, which can't be trapped. Create temporary files in /tmp
, which is normally cleaned out when the system boots. All that remains then is to teach people not to use kill -9
when they don't need to, but that appears to be an uphill battle.
通常的做法是设置一个信号处理程序,以便在程序中断时进行清理。这不会处理kill -9
或物理重启,这是无法被困住的。在 中创建临时文件/tmp
,通常在系统启动时清除。剩下的就是教人们不要在kill -9
不需要时使用,但这似乎是一场艰苦的战斗。
回答by glglgl
If you don't want to use tmpfile()
, you can unlink()
your file immediately after creating it. It will stay open and present and allocated until it is closed.
如果您不想使用tmpfile()
,则可以unlink()
在创建文件后立即使用。它将保持打开和呈现并分配,直到它关闭。
But on a hard reboot, a fsck might be needed in order to recover the space. But as this is always the case, it is no special drawback of this approach.
但是在硬重启时,可能需要 fsck 来恢复空间。但由于情况总是如此,因此这种方法没有什么特别的缺点。