linux /tmp folder + how to know if files will deleted after reboot or after some time

7.5k views Asked by At

I have Linux red-hat machine And I not sure what the concept about the directory /tmp

How to know if the files under /tmp will deleted after reboot or maybe will deleted after some time

Which file/configuration in my Linux machine responsible for that ? And if it possible to change the rules there?

remark my crontab is empty - no deleted Job there

4

There are 4 answers

0
Basile Starynkevitch On BEST ANSWER

This is specified in the File Hierarchy Standard and Linux Standard Base

/tmp/ is often tmpfs mounted, and on systems where it is not the case, the boot init scripts should (and usually do) clean it.

So files under /tmp/ do not survive a reboot. Put them elsewhere (perhaps /var/tmp/) if you want them to survive a reboot.


In the FHS §2.3:

The /tmp directory must be made available for programs that require temporary files.

Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.

Tip Rationale

IEEE standard P1003.2 (POSIX, part 2) makes requirements that are similar to the above section.

Although data stored in /tmp may be deleted in a site-specific manner, it is recommended that files and directories located in /tmp be deleted whenever the system is booted.


So unless your systems are very badly misconfigured, you should presume that /tmp/ is cleaned at least at reboot time. BTW, some sysadmins are setting a crontab entry to clean old files (e.g. weekly clean older than 2 weeks file). See also tmpfiles.d(5), TMPDIR, mkstemp(3), crontab(5), POSIX tmpfile & tmpnam

2
SzG On

Just check the output of

mount

If you find that /tmp is of tmpfs type, then it will be deleted. tmpfs is an in-memory filesystem.

But never count on /tmp to persist.

0
Harini On

The default setting that tells your system to clear /tmp at reboot is held in the /etc/default/rcS file.

The value we’ll look at is TMPTIME.The current value of TMPTIME=0 says delete files at reboot despite the age of the file.Changing this value to a different (positive) number will change the number of days a file can survive in /tmp.

Code:

TMPTIME=7

This setting would allow files to stay in /tmp until they are a week old, and then delete them on the next reboot.

A negative number (

TMPTIME=-1

) tells the system to never delete anything in /tmp.

0
logbasex On

systemctl cat systemd-tmpfiles-clean.timer

# /lib/systemd/system/systemd-tmpfiles-clean.timer
#  SPDX-License-Identifier: LGPL-2.1-or-later
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
ConditionPathExists=!/etc/initrd-release

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d

The [Timer] section specifies what service to trigger (systemd-tmpfiles-clean.timer) and when to trigger it. In this case, the option OnBootSec specifies a monotonic timer that triggers the service 5 minutes after the system boot, while the option OnUnitActiveSec triggers the service 24 hours after the service has been activated (that is, the timer will trigger the service once a day).