Happy Friday! Last week we built a secure directory. Today, we are going one level deeper - we are going to make that directory react on its own.
This is a "Superboss" challenge because it pushes you beyond basic RHCSA tasks and into modern systems engineering. We are still in the objective "Deploy, configure, and maintain systems", but this time with a real-world twist.
Most people use cron for automation. But cron has one big flaw: it waits. If your job runs every minute, a user might wait 59 seconds before anything happens.
Today you will design an event-driven system that reacts the instant a file appears.
You manage a shared directory: /srv/uploads. Users from Windows (Samba) and Linux (SCP/SFTP) drop raw image files in it constantly.
Your architecture must support:
/usr/local/bin/process_images.sh.You will be using deeper systemd features today. Your guides:
man systemd.path - your secret weaponman systemd.serviceman systemd.unitTo build this, you must create two linked units. Share your configuration in the comments.
The service file hotfolder.service runs your script.
[Service] section look like?[Install] section?The path file hotfolder.path detects file changes.
[Path] section to watch for new or changed files in /srv/uploads - PathChanged= or PathModified=?systemctl enable --now to start the monitoring?Let’s see your unit files.
# /etc/systemd/system/hotfolder.service
[Unit]
Description=Process new images dropped in /srv/uploads
[Service]
Type=simple
ExecStart=/usr/local/bin/process_images.sh
--------------------
NOT need an [Install] section because the service is not meant to be started directly or enabled on boot.
It is launched only when the .path unit triggers it.
systemd.path automatically calls the service, so no [Install] section is required.
You must use:
PathModified=
Because:
PathChanged= detects metadata changes (permissions, ownership, rename)
PathModified= detects content creation or modification
# /etc/systemd/system/hotfolder.path
[Unit]
Description=Watch /srv/uploads and trigger image processing
[Path]
PathModified=/srv/uploads
# Optional but recommended: check directory exists at boot
DirectoryNotEmpty=/srv/uploads
[Install]
WantedBy=multi-user.target
###
It will automatically trigger:
hotfolder.service
----------------------
Enable:
systemctl enable --now hotfolder.path
Step 1 - The Service:
1) Create a service file as /usr/lib/systemd/system/hotfolder.service
[Unit]
Description=Log file changes in /srv/uploads
[Service]
Type=simple
ExecStart=/usr/local/bin/process_images.sh
2) The service does not need an [Install] section as it is not a dependency of any specific systemd target.
Step 2 - The Path Unit:
1) Create the path file as /usr/lib/systemd/system/hotfolder.path
[Unit]
Description=Check for file changes in /srv/uploads
[Path]
PathModified=/srv/uploads
Unit=hotfolder.service
PathModified is preferable to PathChanged. PathChanged does not detect every write to a file; only when a file is close after being opened. PathModified will detect simple writes.
2) The Path unit knows which unit to trigger as the [Unit] in the [Path] section specifies the hotfolder.service.
Step 3 - Activation:
1) Running systemctl enable --now hotfolder.path will start the monitoring. Although the hotfolder.service is not enabled, it can still be activated by hotfolder.path.
Note:
Don't forget to create the /srv/uploads directory and the /usr/local/bin/process_images.sh script to complete the service and it's related functionality!
I used the following script to log the changes:
#!/usr/bin/env bash
logger user.info "File created or modified in /srv/uploads"
Hi
Good one. Please find details
Step 1: The Worker (Service)
The service file hotfolder.service runs your script.
What does the [Service] section look like?
Under /etc/systemd/system/hotfolder.service
[Unit]
Description=Executes script when a file get appeared.
[Service]
Type=simple
ExecStart=/usr/local/bin/process_images.sh
Why does this service not need an [Install] section?
A service unit doesn’t need an [Install] section unless it’s meant to be enabled for automatic startup. If it’s only started manually or triggered by dependencies, systemd doesn’t require [Install].
Step 2: The Watcher (Path Unit)
The path file hotfolder.path detects file changes.
Which option will you use in the [Path] section to watch for new or changed files in /srv/uploads - PathChanged= or PathModified=?
***PathModified since requirement is as soon as a file appears, the system detects it.
pathchanged vs pathmodified
- PathChanged=
- Triggered when the contents of a file change (write, truncate, etc.).
- It does not react to metadata-only changes (like permissions or timestamps).
- Example: Writing new lines to a log file.
- PathModified=
- Triggered when a file is modified in any way, including metadata changes.
- This means it reacts to broader changes: content, permissions, ownership, timestamps.
- Example: chmod or touch on a file will trigger it, even if the file’s contents remain the same.
How does this Path unit know which Service unit to trigger when it sees a file?
[Unit]
Description=Monitor the file for changes
[Path]
PathModified=/srv/uploads
Unit=hotfolder.service
[Install]
WantedBy=multi-user.target
Below is flow
- You enable/start the .path unit:
# systemctl enable --now hotfolder.path
- Systemd begins monitoring /srv/uploads for modifications.
- When the condition (PathModified) is met:
- Systemd looks at the Unit= directive.
- It then starts hotfolder.service.
Step 3: Activation
Which unit do you run with systemctl enable --now to start the monitoring?
$ sudo systemctl enable --now hotfolder.path
$ sudo systemctl start hotfolder.path
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.