[rhn-users] auto cleanup of /tmp on boot

Corne Beerse cbeerse at gmail.com
Fri Sep 1 11:23:31 UTC 2006


Sterling, James A wrote:

>And to add to this thread if I may
>
>I have a very simple script that cleans out tmp of files and directories
>that are 'older' then a day.
>
>==========================================================
>#!/sbin/sh
>#
># just to clean out tmp of any files older then one day
>#
># simple .. cd to tmp and then do a find with mtime +1 and remove same
>cd /tmp
>find . -type f -mtime +1 -exec rm {} \;
>find . -type d -mtime +1 -exec rm -fr {} \;
>==========================================================
>  
>

You might have a problem here: A directory is modified if a file or 
subdirectory is created or removed. Think about the next: create a 
subdirectory, store a file in there and then frequently modify the file. 
After a day, your script will remove the tree, including the maybe still 
editable file. It would be better if your second line did an `rmdir` 
because this fails if the directory is not empty. The previous find will 
remove all 'old' files and optionally leave empty directories. The last 
line will remove the empty directories one day after they are cleared. 
It would be better to search for empty directories.

The second, but not so large problem is that empty directories are only 
removed a day after they are cleared, this because they are modified if 
the are cleared. This is not a problem but it is not as one expects.

An other problem is that you test for modification. It would be better 
to test for access. It is fairly usefull to create a file once and keep 
reading it afterwards. Think about pid-files that are used by deamons 
and such: A script starts the deamon process and writes its pid in a 
file. As long as the file exists and the containing pid is a running 
process, it is likely that the process still runs. As soon as the pid is 
not a running process, it has somehow stopped or finished. Once the pid 
file does not exist, it is likely the system is rebooted. In both cases 
the script will start the next process. Here your script will ruin your 
day...

Hence, be carefull with automatic clearing of files and folders in /tmp, 
specially at not-boot times.

On the other hand, should be no problem to clear the contents of /tmp at 
boot time. Leaving relative new files and subdirectories there after a 
boot can be handsome with debugging and such.


Regards

CBee

>It is looking for files or directories that are older then one day and
>then removes them
>I run this from a adm cron job.. And it seems to work well 
>
>Hope this helps.. 
>
>Jim Sterling
>
>-----Original Message-----
>From: Corne Beerse [mailto:cbeerse at gmail.com] 
>Sent: Thursday, August 31, 2006 11:04 AM
>To: Red Hat Network Users List
>Subject: Re: [rhn-users] auto cleanup of /tmp on boot
>
>allen.jordan at convergys.com wrote:
>
>  
>
>>Is there something already in place in the OS that will clean up /tmp 
>>on a boot?
>>Or do I need to add my own script?
>>If so, does anyone have a script?
>> 
>>
>>    
>>
>
>For what it is worth: the long-time unix default is to remove all files
>from /tmp on boot. Directories in there tend to stay there with their
>contents. It is in one of the `rc` files (see /etc/init.d) and it should
>be called relative early in the boot process. However, it should not be
>passed when booting to single-user mode: thats a location to trace stuff
>where the previous contents of /tmp can come in handy.
>
>My opinion is that it should also remove the subdirectories (the entire
>tree below /tmp) and one should expect that: Lots of current
>implementations have /tmp on a filesystem that is cleared on boot. For
>example on a ramdisk or such.
>
>If a tool needs a boot-save temporary storage, use /var/tmp. That is
>there for ages too: vi uses it for example.
>
>
>Regards
>
>CBee
>
>_______________________________________________
>rhn-users mailing list
>rhn-users at redhat.com
>https://www.redhat.com/mailman/listinfo/rhn-users
>
>  
>




More information about the rhn-users mailing list