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.
Aw Tudor, by your providing the ingredients, all that was left for us to do was
to add water and stir
The service file hotfolder.service runs your script. I know the assignment is making
reference to the .service unit file running a script, but I'm going to deviate from that, and
have my actual alerts executed from within the .service unit file itself. Due to the
of my submission, I want to do something to make up for the point deductions
[Service]
Type=oneshot
ExecStart=/usr/bin/bash -c 'mail -s "Hot Folder Alert" trev1 < /tmp/alert_message'
ExecStart=/usr/bin/bash -c 'echo "A change happened in the /srv/uploads directory" > /var/log/uploads'
ExecStart=/usr/bin/bash -c 'echo "The change occured at $(date) - Check it out\n\n" >> /var/log/uploads'
Notes:
1) I'm using "oneshot" as my value for the Type= directive because I have multiple
ExecStart= directives in the [Service] section
2) The user account "trev1" is one of the admin accounts on my system, and so when
any changes occur in the /usr/uploads directory, or any modifications occur to any files
in that same directory, an email will be sent to that user account. Also, I'm go to have
a couple of messages sent (logged) to a file named /var/log/uploads. The messages
will simple reflect that something has occured in the /srv/uploads directory, along with
the date and time that it occured, and that "trev1" shouid stop what he's doing, and
have a look at exactly what has caused this alert.
2. Why does this service not need an [Install] section?
The path file hotfolder.path detects file changes.
The directive that I will use, in the [Path] section, to watch for new or changed files in
the /srv/uploads directory, is the PathModified= directive. My reason for using this
directive is because it performs everything that the PathChanged= directive does, and additionally, activates its dependent .service unit file - in this case hotfolder.service - on
simple writes to content in the directory that's being monitored.
2. How does this Path unit know which Service unit to trigger when it sees a file?
The .path unit file will know which .service unit to trigger, based on the configuration of
the Unit=argument directive. The argument in the directive is a unit name, whose suffix not ".path".
Note. If the argument to the Unit= directive is not specified, the value defaults to a
service that has the same name as the .path unit file (e.g. path-unit-file-prefix.service).
My research involving the Unit= directive, recommends that the argument of the Unit=
directive be explicitly named (i.e. the same prefix as the .path unit file, with a suffix of
.service).
Of all the steps this was the most challenging to respond to
Here are my unit files:
hotfolder.service
[Unit]
Description=Sends an email to the admin of this system when a change is made to
Description=/srv/uploads directory
[Service]
Type=oneshot
ExecStart=/usr/bin/bash -c 'mail -s "Hot Folder Alert" trev1 < /tmp/alert_message'
ExecStart=/usr/bin/bash -c 'echo "A change happened in the /srv/uploads directory" > /var/log/uploads'
ExecStart=/usr/bin/bash -c 'echo "The change occured at $(date) - Check it out\n\n" >> /var/log/uploads'
hotfolder.path
[Unit]
Description=This unit file will monitor changes in the /srv/uploads directory
[Path]
PathModified=/srv/uploads
Unit=hotfolder.service
[Install]
WantedBy=multi-user.target
Note: In my holder.path unit file, the argument (value) that I'm using for the
PathModified= directive is the directory name:
Okay, this will conclude my response. I've already penalized myself with a -10 points
late submission. Am I still be too lenient
Thanks Tudor for the exercise!!!
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.