In this video, you'll learn about managing temporary files, where we have a special mechanism
as well.
In the past, temporary files were created in the slash TMP directory.
Without management, these files could stay around for a long time.
And there are multiple solutions to take care of that.
One of the solutions is to mount the TMP directory on a RAM drive.
A RAM drive is temporary, so every time you reboot your system, the entire TMP directory
is gone.
There's another alternative, and that is systemd.tmp files.
So systemd.tmp files started while booting, and it manages temporary files and directories.
And functionality has been expanded to other files as well.
So you can use systemd.tmp files to manage any file that should exist on a temporary
basis.
It will create and delete TMP files automatically, according to the configuration files, which
are in usuallib.tmpfiles.d, etc.tmpfiles.d, and run.tmpfiles.d.
You have guessed usuallib is for the standard provided, etc. is for your customizations,
and run.tmpfiles is for dynamically generated files.
systemd.tmpfiles works with related services to manage temporary files.
One of them is systemd.tmpfiles-setup, which creates and removes temporary files according
to the configuration.
Then there is systemd.tmpfiles-clean, which calls the systemd.tmpfiles-clean service to
remove temporary files.
And by default, this happens 15 minutes after booting, and also on a daily basis.
So how do you manage these temporary files?
Well, in the configuration files, in usuallib.tmpfiles.d and etc.tmpfiles.d, you specify what to do
with your temporary files.
You can use lines like d run myfiles 0750 root root, which will create the directory
run myfiles if necessary, and no action will happen if it already exists.
uppercase D, with the same argument, will create the directory if necessary and wipe
its content if it already exists.
And files older than 1D, that's what the 1D is doing at the end, are eligible for automatic
removal the next time that systemd.tmpfiles runs.
For more information, have a look at man page.
The man page tmpfiles.d provides detailed information as well as examples.
Let me run a demo.
Here's part one of the demo.
So I'm starting with echo q slash tmp1777 root root 7d.
And I write that to etc.tmpfiles.d tmp.conf.
So that will create a configuration that creates a tmp directory and removes all unused files
after seven days.
Now I'm going to use systemd.tmpfiles minus minus clean slash etc.tmpfiles.d tmp.conf.
That's a syntax check.
And it's not complaining, so syntax is OK.
So now I'm using echo d tmpmyfiles0770 root root 30s.
And I'm writing that to etc.tmpfiles.d myfiles.conf.
Next, I'm using ls minus ld tmpmyfiles.
And we see no search file or directory.
Well, that makes sense because we haven't done anything.
Next, I'm using systemd.tmpfiles minus minus create etc.tmpfiles.d myfiles.conf.
And I'm using my ls minus ld again.
And as you can see, it was created.
Well, that's very nice.
Using touch on tmpmyfiles test.
And what do we see?
Well, that's simple.
I made a typo.
I forgot the slash between tmp and myfiles.
This is working so much better.
So part two of the demo, let me show you the slide that is part two,
is about what we see here.
So let me show you.
So now I'm using ls minus l tmpmyfiles.
And there we can see test.
And then I'm going to sleep for 30 seconds.
And then ls minus l again.
And oh, it still exists.
Does that make sense?
Yes, because we never ran systemd.tmpfiles minus minus clean on etc.tmpfiles.d myfiles.conf.
So now it's processing that file.
And after processing, we get the total zero.
And the directory has been cleared.