leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I am generating temp files.. I know that i can use a cron job, to clean up. I was wondering.. is there some place on a linux box where any user can naturally put temp files? does the tmp dir work like that? so i can gerenate temp junk endlessly without worry of clean up ?

Also I don't want to be a buttmunch asking this stuff in the wrong place.. is there a more appropriate (although i already know not better.. ) place where i can ask unix/linux questions that could be suggested? I'm having a hard time finding a good place for this. Thank you ahead for any help. I hope I don't annoy anyone with this.

  • Comment on (OT) place on linux box to safely create tons of temp junk???

Replies are listed 'Best First'.
Re: (OT) place on linux box to safely create tons of temp junk???
by idsfa (Vicar) on Apr 02, 2006 at 23:59 UTC

    Sick Unix Trick #347 ... ghost files.

    # Open the file, creating it open $fh, '+>', 'foo.txt'; # Immediately unlink the file ... it will remain # until you close the filehandle, then disappear unlink 'foo.txt'; # Put something in it to test print $fh "Foo\n"; # Rewind to the start of the temp file seek $fh, 0, 0; # Read it back in and see what you have $in = <$fh>; print "Read: $in\n"; # You don't even *have* to remember to close it, # but it is a good habit ... close $fh;

    Updated: Note that unlike File::Temp, this trick will clean up after itself no matter what happens to the process. It does not depend upon DESTROY being invoked.


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
Re: (OT) place on linux box to safely create tons of temp junk???
by Zaxo (Archbishop) on Apr 03, 2006 at 01:21 UTC

    By unix rules, the system normally should not remove anything from /tmp while running. In /tmp any program is supposed to be able to create files, write to them, and assume that files placed there will remain until removed by the owner. A program can not assume that the contents of /tmp will survive a reboot. That makes startup scripts the place for the system to do /tmp cleanup.

    Some admins put in place some other policy they dream up, but the unix police will someday get 'em.

    Putting /tmp on its own spindle, or at least a large partition, and mounting with the sticky bit set fixes most problems that non-unix policies are meant to solve. Going all BOFH on programs which don't clean up after themselves takes care of the rest.

    Of course, users may clean up their own messes any time they like.

    After Compline,
    Zaxo

Re: (OT) place on linux box to safely create tons of temp junk???
by perrin (Chancellor) on Apr 02, 2006 at 20:28 UTC
Re: (OT) place on linux box to safely create tons of temp junk???
by sgifford (Prior) on Apr 02, 2006 at 23:27 UTC
    Whether or not any directories on the system have old files removed periodically depends on the system's configuration, or if you haven't configured it, its defaults. So the first thing to do is poke around inside your system's documentation and/or cron configuration and see if it's already cleaning anything. If not, set it up to automatically clean /tmp, then write your files there.

    File::Temp is a useful way to create tempfiles, but you'll still have to figure out how to get them cleaned up.

    Also, it's best if you can clean them up yourself, and only leave them lying around in the rare case that your program crashes. Things like END blocks are useful places for this sort of cleanup.

      Actually, File::Temp will clean up after you if you want it to.
        Yes indeed, I didn't know that! Very handy. Not entirely foolproof, for example if your process crashes perl or is killed with SIGKILL, but probably close enough for most purposes.
      About cleaning them up myself.. I am creating some modules, and i have distribution in mind. I only trust people downloading and configuring so far- installing a module, and setting up a cron, may be too much to expect from some people.
        Then just let them configure where temp files go, defaulting to /tmp, try your best to clean them up when you exit, and document that some systems won't automatically clean that area, and on the rare occasion something goes Very Wrong, the user may need to clean it manually once in awhile, or set up a cronjob to do it automatically. That's really all you can do.
Re: (OT) place on linux box to safely create tons of temp junk???
by chrism01 (Friar) on Apr 03, 2006 at 01:58 UTC
    In re where to ask Unix/Linux qns, try www.linuxquestions.org, I think you'll like it.
    Of course there are plenty of Unix/Linux monks here...
      Of course there are plenty of Unix/Linux monks here...
      In which case, we prefer you to use the Chatterbox to ask quick questions.