Friday, November 12, 2021

Understanding Temp Files in Linux - Part 1

                         ဒီ article မှာတော့ Linux system တစ်ခုမှာ ရှိတဲ့ temp files တွေကို ဘယ်လိုမျိုး system က regularly clean up လုပ်လဲဆိုတာ ပြောသွားမှာဖြစ်ပါတယ်။

What is temp files?

                        Temp files တွေဆိုတာကတော့ system ပေါ်မှာ application service တစ်ခုခု ကနေ generate လုပ်လာတဲ့ temp files အမျိုးအစားတွေ ဖြစ်ကောင်းဖြစ်မယ် ဒါမှမဟုတ် system ကိုယ်တိုင်က  generate လုပ်တဲ့ temp files တွေလဲ ဖြစ်မယ်။ တစ်ချို့ temp files တွေက Memory အပေါ်မှာပဲ running ဖြစ်နေပြီး system reboot ဖြစ်သွားတဲ့အခါ မရှိတော့ဘူး တစ်ချို့ temp files တွေကတော့ system ကနေ clean up လုပ်ပေးမှ clear ဖြစ်တဲ့ temp files အမျိုးအစားတွေလဲရှိပါတယ်။ သေချာတာကတော့  ဒီ temp files တွေဆိုတာ system တစ်ခုအတွက် လိုအပ်သလို ဒီ temp files တွေကြောင့်လဲ system ရဲ့ storage space တွေ မလိုအပ်ပဲ waste ဖြစ်နိုင်ပါတယ်။ ဒါကြောင့် Linux or Windows မှာပဲဖြစ်ဖြစ် ဒီ temp files တွေကို clean up လုပ်ဖို့အတွက် လိုအပ်ပါတယ်။ 

Where are the tmp files located ?

                        Linux မှာတော့ temp files တွေကို  ဒီ directories  "/var/tmp", "/tmp", "/run" တွေ အောက်မှာ default အားဖြင့် တွေ့ရပြီး "/run" directory အောက်မှာရှိတဲ့ temp files တွေကတော့  across reboot မှာ persistent မဖြစ်ပါဘူး။ "/var/tmp" and "/tmp" directory တွေမှာရှိတဲ့ temp files တွေကတော့ သူ့ရဲ့ အချိန်ကာလတစ်ခုအထိ system ပေါ်မှာ persistent ရှိနေမှာဖြစ်ပါတယ်။ 

Who is responsible for cleaning up the temp files?

                        ဆိုတော့ ဒီ persistent ဖြစ်တဲ့ temp files တွေကို ဘယ်သူက တာ၀န်ယူပေးလဲဆိုရင်  "systemd-tempfiles" က တာ၀န်ယူပေးပါတယ်။ အောက်ပုံမှာ systemd-tempfiles နဲ့ပတ်သတ်တာတွေကို ပြထားပေးပါတယ်။

                        "systemd-tempfiles" ကမှတစ်ဆင့် "systemd-tmpfiles-clean.timer" မှာ ဘယ်အချိန်မှာတော့ clean up လုပ်ပါဆိုပြီး schedule သတ်မှတ်ကာ schedule သတ်မှတ်သည့်အတိုင်း ကို "systemd-tmpfiles-clean.service" က clean up လုပ်ပေးပါတယ်။ 

Let's see the current config of systemd-tmpfiles-clean.timer

                    အောက်ပုံမှာပြထားသည့်အတိုင်း "systemd-tmpfiles-clean.timer" ရဲ့ config ကိုကြည့်ရင် [Timer] ဆိုတဲ့ section အောက်မှာ "OnBootSec=15min"  ဆိုတာကတော့ System boot တက်ပြီးပြီးချင်း "15min" နေရင် temp files တွေကို clean up စတင်လုပ်ဆောင်မယ်လို့ ပြောတာဖြစ်ပြီး  "OnUnitActive=1d" ကတော့နောက်ထပ် clean up process ကို 24hr ကျော်ပြီးနောက်မှာ ထပ်ပြီး run မယ်လို့ ပြောတာဖြစ်ပါတယ်။ လက်ရှိ config ကို ပြင်ချင်ရင်တော့ "/usr/lib/systemd/system/systemd-tmpfiles-clean.timer" ထဲမှာ ၀င်ပြင်ရမှာဖြစ်ပါတယ်။




So, Where is the configurations file for systemd-tmpfiles?

                    Systemd-tmpfiles ကို အသုံးပြုပြီး temp files တွေကို ရှင်းလင်းဖို့အတွက် configuration files တွေကို ဘယ်နေရာမှာသိမ်းထားလဲဆိုရင် အရင်ဆုံး "man tmpfiles.d" ကို ဖွင့်ကြည့်မယ်။ အဲ့ဒီမှာ "SYNOPSIS Section" အောက်မှာ ဖော်ပြထားတဲ့ 

1. /etc/tmpfiles.d/

2. /run/tmpfiles.d/

3. /usr/lib/tmpfiles.d/ 

ဆိုတဲ့ directories တွေအောက်မှာ systemd-tmpfiles အတွက် configuration files တွေကို ထားရှိပါတယ်။ အဲ့ဒီ့ configuration တွေကို systemd-tmpfiles က ယူသုံးပြီး clean up ပြုလုပ်ပေးတာဖြစ်ပါတယ်။ System က first priority အနေနဲ့ /etc/tmpfiles.d အောက်မှာရှိတဲ့ config files တွေကို ကြည့်မှာဖြစ်ပြီး အကယ်၍  နာမည် တူ config file ကို /etc/tmpfiles.d နဲ့ တစ်ခြား နှစ်ခုမှာလဲ ရှိနေခဲ့ရင် /etc/tmpfiles.d မှာ သတ်မှတ်ထားတဲ့ config က overwrite လုပ်နိုင်ပါတယ်။ 2nd priority အနေနဲ့ "/run/tmpfiles.d" ဖြစ်ပြီး last priority ကတော့ "/usr/lib/tmpfiles.d" အောက်မှာ ရှိတဲ့ config files တွေဖြစ်ပါတယ်။

Let's take a look at these directories

                        လက်ရှိမှာတော့ "/etc/tmpfiles.d" နဲ့  "/run/tmpfiles.d" directories အောက်မှာ config files တွေ သိပ်မရှိသေးဘူး။ "/usr/lib/tmpfiles.d" directory အောက်မှာတော့ temp files clean up လုပ်ဖို့အတွက် configuration files တော်တော်များများကို တွေ့ရမှာပါ။ Sample အနေနဲ့ "/var/tmp" and "/tmp" directory တွေအတွက် define လုပ်ထားတဲ့ "tmp.conf" config file ကို ကြည့်မယ်ဆိုရင် 

1. q /tmp 1777 root root 10d = /tmp directory အောက်မှာ ရှိတဲ့ files တွေကို 10 days ကျော်ရင်  clean လုပ်ဖို့ ညွှန်ကြားထားတာဖြစ်ပြီး

2. q /var/tmp 1777 root root 30d = /var/tmp directory အောက်က files တွေကိုတော့ ရက် ၃၀ ကျော်ရင် clean လုပ်ဖို့ define လုပ်ပေးထားတာဖြစ်ပါတယ်။



                    Part-1 ကို ဒီလောက်နဲ့ပဲရပ်ထားပြီး Part-2 မှာတော့ config file မှာသုံးထားတဲ့ syntax ပုံစံတွေ နောက် temp files တွေ အတွက် custom directory ဘယ်လို တည်ဆောက်မလဲ, auto and manul temp files cleaning လုပ်ပုံတွေကို ဆက်သွားပါမယ်။

That's it!
Thanks and Enjoy Reading!!! :) 
Follow and Like Root Of Info Page and Root Of Info Youtube Channel.

Share:

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.