هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
# Type Path Mode User Group Age
d /run/myapp 0750 myapp myapp -
d /var/cache/myapp 0750 myapp myapp 7dsystemd-tmpfiles --create /etc/tmpfiles.d/myapp.conf
systemd-tmpfiles --clean /etc/tmpfiles.d/myapp.conf
systemctl list-timers systemd-tmpfiles-clean.timerStep by step
- Write the file (quick answer) — one line per path: type, path, mode, user, group, age.
d— create the folder; with an age, clean its content.D— liked, but also empty it at every boot.e— do not create, only adjust and clean an existing folder.f— create a file;L— a symlink;r/R— remove;x— exclude a path from cleaning.
- Create now (line 1 of the commands) — at boot this runs automatically (
systemd-tmpfiles-setup.service). - Clean now (line 2) — deletes files in
/var/cache/myappthat were not used (read, written or changed) for 7 days. Later this runs daily viasystemd-tmpfiles-clean.timer(line 3). - Ages —
30m,12h,7d,4w.-= never clean. - Change a system default — the distribution's rules are in
/usr/lib/tmpfiles.d/; copy a file with the same name to/etc/tmpfiles.d/and edit it there (the/etccopy wins). For example/tmpcleaning age intmp.conf. - For a service, even simpler — if only the service needs
/run/myapp, useRuntimeDirectory=myappin the unit; systemd creates and removes it with the service. - See what would happen —
systemd-tmpfiles --cat-configshows all rules that apply, in order.
Check that it worked
ls -ld /run/myapp shows drwxr-x--- myapp myapp; after a reboot the folder is there again; to test the cleaning, give a test folder a short age (for example 1m), create a file, wait two minutes, create a second file and run --clean: the old file is gone, the new one stays. (touch -d alone does not work for this test: tmpfiles also looks at the change time, which touch cannot set back.)
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Folder missing after reboot | No tmpfiles rule (only created by hand). | Add a d line; check systemd-tmpfiles-setup status. |
| Files not cleaned | No age, or the files were read or changed recently (atime, mtime and ctime all count). | Set an age; know that access and metadata changes also count. |
| Too much deleted | Age too short, or a path in a shared folder. | Longer age; exclude paths with x. |
Failed to resolve user | User does not exist yet. | Create the user first (or with sysusers.d). |
Change to a /usr/lib file lost after update | Edited the package file. | Copy to /etc/tmpfiles.d/ and edit there. |
| Cron job and tmpfiles both clean | Two tools for one job. | Keep only tmpfiles. |
Go further
- Command pages: systemd-tmpfiles, systemctl.
- Next how-tos: Create a systemd service for your app, Create a systemd timer, Check disk space and find what fills the disk, Rotate log files with logrotate.
- Learn it: RH134 — tmpfiles.
1Why is a folder under /run gone after a reboot?
/run is in memory (tmpfs); a tmpfiles rule creates the folder again at every boot.
2What does the age field do in a tmpfiles rule?
Files in that folder older than the age are deleted when the cleaning runs (daily by a timer).