LoginSignup
666

More than 5 years have passed since last update.

/tmpと/var/tmpの仁義無き戦い

Last updated at Posted at 2014-03-30

課題

/tmpと/var/tmpどっちも大体一緒だからいいんじゃないかと思って/tmpにファイルをつくろうとしたら、プログラムが使用するものは/var/tmpにと叱られた。確かに、基幹系システムのディストリビューションだと何故か/var/tmp派の人が多かった気がする。じゃあ、linux系特有の宗派の問題なのか?と思い調べてみた。

何が他のディレクトリと違うか

  • 通常のディレクトリは、基本的にはファイルは削除しない限り消えない。
  • /tmpに関しては再起動するとファイルが綺麗さっぱり無くなる。
  • /var/tmpは再起動しても消えないがいつの間にかファイルが消えることがある。

うーん、やはり使用してきたディストリビューションのルールか、業務の慣例的なルールなのかなぁ。

やはり/tmpと/var/tmpは宗教論争なの?

そういうわけではないらしい。FHS(Filesystem Hierarchy Standard)でちゃんと決めてくれているようだ。

FHSv2.3:現行標準 (2014/3/29時点)

/tmp

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.

  • /tmpは、一時的ファイルを必要とするプログラムがいつでも使用できるようにしておくこと。
  • プログラムは、次回の起動時にファイルまたはディレクトリが/tmpの中に残っていることを前提にしてはならない。

/var/tmp

The /var/tmp directory is made available for programs that require temporary files or directories that are preserved between system reboots. Therefore, data stored in /var tmp is more persistent than data in /tmp. Files and directories located in /var/tmp must not be deleted when the system is booted. Although data stored in /var/tmp is typically deleted in a site-specific manner, it is recommended that deletions occur at a less frequent interval than /tmp.

  • /var/tmpは、一時ファイルやディレクトリを必要とするプログラムで利用され、システムを再起動しても削除されない。従って、/var/tmpのデータは/tmpのデータよりも長く保持される。
  • /var/tmpに置かれるファイルやディレクトリはシステム再起動の時に削除してはならない。通常、/vat/tmpのデータはサイト固有のルールで削除されるが、基本的には/tmpよりも削除の頻度を低くすることが望ましい。

FSSTND v1.0:現在は未サポート

すでにサポートされていないが、初期ではどのような定義をされていたのかFSSTND v1.0で確認してみる。

3.11. /tmp : temporary files

/tmp is used for temporary files, preferably on a fast device (a memory based filesystem, for instance). The "persistence" of the data that is stored in /tmp is different from that which is stored in /var/tmp. /tmp is usually cleaned out at boot time (or at relatively frequent intervals). Because of this, data stored in /tmp should not be expected to remain for any long period of time on the system... it is frequently deleted. Programs should use /tmp or /var/tmp (which was originally /usr/tmp) according to the expected persistence of the data, but should not rely on any particular persistence for any tmp directories. The precise arrangement of /tmp and /var/tmp is a local issue. If there are distinct /var/tmp and /tmp directories, then the persistence of /var/tmp files should be at least as long as for /tmp, but beyond that, a site administrator or distributor can arrange this any way they want, over RAM disk, symlinks, or whatever.

  • /tmpは、メモリベースのファイルシステムなど高速なデバイス上で使用されることが多い
  • /tmpに置かれるデータの保持期間は/var/tmpとは異なる。/tmpは、起動時または比較的頻繁にクリーンアップされる。このため、/tmpのデータが長期間残っていることを前提にすべきではない。かなり頻繁に削除されると思うべき。
  • プログラムで/tmpまたは/var/tmpのどちらを使用すべきかはデータの使用期間によるが、いずれの場合もデータが永続化されることを前提にすべきではない。
  • /tmpや/var/tmpの使い分けは厳密ではない。/tmpと/var/tmpの違いのうち、/var/tmpファイルの保存期間は/tmpより短くしてはならないことだけは確かである。それさえ押さえていれば、サイト管理者やディストリビュータはRAMディスクやシンボリックリンクなども含めて保存ポリシーを自由に設定して良い。

5.7. /var/tmp : temporary files, used to keep /tmp small

Files in /var/tmp are stored for an unspecified duration (please remember that system temporary directories are not guaranteed to hold data for any particular duration). Data stored in /var/tmp is typically cleaned out "in a site-specific manner", but usually at less frequent intervals than /tmp. More information on temporary directories is in the section of the standard devoted to /tmp (above). /usr/tmp is usually symlinked to /var/tmp for compatibility reasons.

  • /var/tmp内のファイルの保持期間は不特定である(システムの一時ディレクトリではいかなる期間であってもデータの保持は保証されていないことに注意)
  • /var/tmp内のデータの削除は、一般的にサイト固有のタイミングで行われるが、/tmpでの削除ほど頻繁でないのが普通。一時ディレクトリの詳細については上の/tmpセクションを参照のこと。
  • /usr/tmpは、互換性のため/var/tmpにシンボリックリンクされるのが普通。

結局のところ/tmpと/var/tmpの特徴は...

ディレクトリ 概要
/tmp メモリの代わりに使用しようとしていた(だから再起動時に消す思想なのではないかと思われる)
一時的ファイルを必要とするプログラムで使用できる
再起動するとファイルは消える
定期的に削除される
/var/tmp 一時的ファイルを必要とするプログラムで使用できる
再起動してもファイルは消えない
/tmpより長い期間保持されるが定期的に削除される

あれ?誰が定期的に消しているの?

CentOS 6.4で確認してみると、/etc/cron.daily/etc/rc.d/rc.sysintの部分で削除していた。

/etc/cron.dailyによると下記のタイミングで消すようだ。 ディストリビューション毎に異なるかもしれないので必ず確認して下さい。

  1. /tmpは、10days
  2. /var/tmpは、30days
/etc/cron.daily
[root@localhost cron.daily]# cat tmpwatch
#! /bin/sh
flags=-umc
/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \
        -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \
        -X '/tmp/hsperfdata_*' 10d /tmp
/usr/sbin/tmpwatch "$flags" 30d /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d "$d" ]; then
        /usr/sbin/tmpwatch "$flags" -f 30d "$d"
    fi
done
/etc/rc.d/rc.sysinit
--- snip ---

# Clean up various /tmp bits
[ -n "$SELINUX_STATE" ] && restorecon /tmp
rm -f /tmp/.X*-lock /tmp/.lock.* /tmp/.gdm_socket /tmp/.s.PGSQL.*
rm -rf /tmp/.X*-unix /tmp/.ICE-unix /tmp/.font-unix /tmp/hsperfdata_* \
       /tmp/kde-* /tmp/ksocket-* /tmp/mc-* /tmp/mcop-* /tmp/orbit-*  \
       /tmp/scrollkeeper-*  /tmp/ssh-* \
       /dev/.in_sysinit

# Make ICE directory
mkdir -m 1777 -p /tmp/.ICE-unix >/dev/null 2>&1
chown root:root /tmp/.ICE-unix
[ -n "$SELINUX_STATE" ] && restorecon /tmp/.ICE-unix >/dev/null 2>&1

--- snip ---

確かに、/var/tmpは/tmpよりも長い期間保持するようになっている

まとめ

うん、 linuxではやっぱりどっちでもいい気がする笑

強いていうならば、再起動して消えてもらっては困りそうなファイルなら/var/tmpに置くらいではないかと思う。逆にやたらと大量にファイルを作るような場合には、ディスク容量など圧迫しないように/tmpとかでも良い気がします。

と思ったら話しには続きが...

Linux以外のUnix系OSでは単純にどっちでもいいわけではないようだ。Operating systemによるとunixは下記の様な系譜になる

unix.png

Linuxは左の方にある独立した系譜になっており、どうもBSDから派生したSunOS, AIX, HP-UXといういわゆる基幹系でよく使用されているOSでは/tmpを特別なものとしてるようだ。

IBM AIXとOracle Solarisのテクノロジー・マッピング・ガイドによるとスワップ領域としても/tmpを使われることが多いらしい。

ディストリビューション 概要
AIX 1. AIXのメモリ管理は、AIX Virtual Memory Manager(VMM)特別な論理ボリュームに割り当てられる。
2. 領域が不適切であると、プロセス全体が喪失したり、システムがクラッシュしたりする可能性がある
Solaris 1. ファイルフォーマットとしてUFS/ZFS/TMPFS等がある
2. 一時ファイルシステム (TMPFS) は、ファイルシステムの読み取りと書き込みにローカルのメモリーを使用するため、一時ファイルシステムは、UFS ファイルシステムに比べはるかに高速らしい
3. TMPFSは再起動するとファイルをクリアする

なるほど、これから推測するにAIXではクラッシュすることがあるため/tmpを使うことを避けて、Solarisだとスワップ領域がいっぱいになることを防ぐため不用意に/tmpへファイルを置かないというルールが出来たのではないかと。

再度まとめ

  1. linuxでは特に気にしなくて良さそう
  2. SunOSやAIX等の場合は/tmpについても使用検討をする必要がある
  3. そのため、SunOSやAIXなどを使用してきた方は/var/tmpにファイルを置くことをルールとしている可能性がある

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
666