1

Is there a "standard" temporary directory directory allocated for an individual process which disappears along with the process itself? Something that looks like /proc/$pid/tmp or /proc/self/tmp. There are numerous temporary directories: system global temporary directories, user specific temporary directories, persistent and those cleared on reboot, tmpfs based, etc.

It sounds like a common sense to have a temporary directory per process yet I can't find one.

2
  • 1
    Why would the kernel allocate extra storage to every process by default? That seems like a lot of overhead (and I/O) for a very tiny minority of processes. You might want to look at man 3 mktemp.
    – DopeGhoti
    Commented Mar 29, 2017 at 18:48
  • Common sense being, as they say, not so common. Here's some flailing on the topic of tmp dirs: "Anyway, I think I fixed the bug. This was one of those tricky and fun ones." -- bugzilla.redhat.com/show_bug.cgi?id=1183684
    – thrig
    Commented Mar 29, 2017 at 19:27

2 Answers 2

2

No, there's no standard or even vaguely common location for a per-process temporary file. The reason is that per-process temporary files are very uncommon. The point of a temporary file is almost always to exchange data between processes. If a process doesn't need to communicate with other processes, then it can keep the data in its memory.

What would make a bit more sense is a per-process temporary directory whose files are also accessible to other processes, but which is removed when the process exits. However that's a pretty expensive operation (in terms of implementation complexity): killing the process would have to perform a recursive file and directory removal, which may do very complex things like causing a mount point disappear. The benefit would be fairly small for something that can be implemented in a couple of lines of shell.

#!/bin/sh
tmpdir=$(mktemp -d) || exit
the_real_program; ret=$?
rm -rf -- "$tmpdir"
exit $ret

Technically, what you want would be possible with FUSE: make the process implement a temporary file filesystem through the FUSE driver. But, again, that's a lot of complexity for little benefit.

1
  • 1
    I agree that if this capability existed it could be expensive. Your example is what normally everyone does. This approach doesn't guarantee file/dir cleanup though. It can die/be killed before the cleanup occurs. Hence the wish of having something like /proc/$pid/tmp. Just like everything under /proc/$pid it would be accessible by processes owned by the same user and it would vanish along with the process. There is nothing wrong with having a wish :) Commented Mar 30, 2017 at 21:20
0

In short: No. The /proc items are memory-based, and it would be better if you use something like /tmp oir /usr/tmp (or similar) with a mktmp or similar command.

It would as mentioned previously be very costly for the kernel to map up space in case, and your machine's memory would soon be exhausted.

Any application that uses temp files should also do some housekeeping, clearing out after itself.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .