متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 222System · systemd

How to clean temporary files with systemd-tmpfiles

An app needs /run/myapp after every boot (it is on tmpfs and gone after a reboot), and its cache folder fills the disk because nobody deletes old files. Instead of a cron job with find … -delete, systemd-tmpfiles does both from one small config file: create folders with the right owner and mode, and remove files older than a given age, every day. This how-to writes such a file and tests it.

  • 5 الدروس
  • 4 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

222.1

The quick answer

/etc/tmpfiles.d/myapp.conf
# Type  Path               Mode  User   Group  Age
d       /run/myapp         0750  myapp  myapp  -
d       /var/cache/myapp   0750  myapp  myapp  7d
systemd-tmpfiles --create /etc/tmpfiles.d/myapp.conf
systemd-tmpfiles --clean /etc/tmpfiles.d/myapp.conf
systemctl list-timers systemd-tmpfiles-clean.timer

222.2

Step by step

  1. 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 — like d, 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.
  2. Create now (line 1 of the commands) — at boot this runs automatically (systemd-tmpfiles-setup.service).
  3. Clean now (line 2) — deletes files in /var/cache/myapp that were not used (read, written or changed) for 7 days. Later this runs daily via systemd-tmpfiles-clean.timer (line 3).
  4. Ages — 30m, 12h, 7d, 4w. - = never clean.
  5. 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 /etc copy wins). For example /tmp cleaning age in tmp.conf.
  6. For a service, even simpler — if only the service needs /run/myapp, use RuntimeDirectory=myapp in the unit; systemd creates and removes it with the service.
  7. See what would happen — systemd-tmpfiles --cat-config shows all rules that apply, in order.

222.3

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.)

الأوامر في هذا الدرس

222.4

Common problems

ProblemCauseFix
Folder missing after rebootNo tmpfiles rule (only created by hand).Add a d line; check systemd-tmpfiles-setup status.
Files not cleanedNo 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 deletedAge too short, or a path in a shared folder.Longer age; exclude paths with x.
Failed to resolve userUser does not exist yet.Create the user first (or with sysusers.d).
Change to a /usr/lib file lost after updateEdited the package file.Copy to /etc/tmpfiles.d/ and edit there.
Cron job and tmpfiles both cleanTwo tools for one job.Keep only tmpfiles.

222.5

Go further

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).

كل الأوامر في هذا الفصل