What are good temp file practices?

What would be considered good temp file practices? Is there like a rule of thumb?

I made a script that creates a temp file. At the moment it saves the temp file in “private/tmp” without deleting it at the end. The assumption being that it will get deleted at the next startup. Is this the correct way of going about?

When I need to make a temporary file I generally will use the shell mktemp command. I also will, generally speaking, try to delete the file as well regardless of the method the file was created with.

As long as the file is relatively small though I see no harm in leaving it in /private/tmp with the expectation that it be deleted upon restart.

Thanks, looks like it’s worth learning a bit of shell scripting in the long run.

Realize some people go long periods of time without restarting - for instance I put my computer to sleep a lot, but only restart once every week or two. So in my case the temp file might hang around for a while - not a problem unless it gets in the way next time I run the script, and it doesn’t know how to handle the file already being there, etc.

I think technically you are supposed to clean up your temp files on your own, but in many cases it would probalby be fine to let the system purge it. I know when I look in the temp folder after several days of uptime, the folder has several things left in it that didn’t get removed by the programs that created them…

See also: writeTempFile