Lab 8: Systemd

Task 1: Create a shell script

You have to manually run this script whenever the system is restarted. We can solve this by creating a systemd service for it. This will give us more options for managing the script’s execution.

Task 2: Create a systemd file

Next, create a systemd service file for the script on your system. This file must have .service extension and saved under the /lib/systemd/system/ directory

$ sudo vim /lib/systemd/system/shellscript.service 

Now, add the following content and update the script filename and location. You can also change the description of the service. After that, save the file and close it.

[Unit] Description=My custom web service to show system processes [Service] ExecStart=/usr/bin/script.sh [Install] WantedBy=multi-user.target

The [Unit] section describes the service, specifies the ordering dependencies, as well as conflicting units. In [Service], a sequence of custom scripts is specified to be executed during unit activation, on stop, and on reload. Finally, the [Install] section lists units that depend on the service.

Task 3: Enable the service

Task 4: Systemd control groups (cgroups)

Systemd control group is a mechanism that allows you to control the use of system resources by a group of processes.

Questions to answer

Upload the scripts you create to moodle.
Show test results of your implementation in your report.

  1. Show the following boot-up performance statistics on your system:

    • Time spent in the kernel space before the user space was reached.
    • Show an SVG image that contains services that have been started, and how long it took for them to initialize.
  2. Take the systemd unit graphical.target as your starting point, start tracing backwards using only the Requires variable. At what systemd unit do you reach a dead end where there is no more Requires variable?

    • Provide brief explanation for each of the systemd units you encounter while performing this trace.
    • The unit at this dead end Wants some systemd units. Why does it want these units?

    Show screenshots of every step as you trace.

  3. Create a simple web server in bash that shows the following: system uptime, inode usage, current memory, disk space usage statistics, and the last 15 lines of /var/log/syslog.

    • The required information should be queried from the server everytime a user opens or refreshes the page.
    • You do not need to save the results anywhere. Users only need live updates when the server is visited.
    • The results should be displayed on a single page in an orderly manner that is easy to read.
    • Create a systemd service on your system to run this script (web server). Show how you can start your new service, and configure it to run after system reboot.
    • Your systemd service should restart the web server if the web server crashes or is killed.
    • This service is allowed to use a maximum of 15% of the CPU and 256MB memory.

    Show all steps taken, and all unit files created in your report.
    At the end of this task, you must have at least one bash script, one service file, and one slice file all working together to achieve the objectives.

  4. Create a systemd service that will update your package sources list from the repository.

    • The service should update the package source list five minutes after booting, and then every day after that.
    • The schedule of the execution should be done with only systemd.

Bonus

  1. Create a custom target in /etc/systemd/system/<your_target>.target.
    • Add a description of the target file.
    • Create a directory /etc/systemd/system/<your_target>.wants/
    • Create sylinks to additional services you wish to enable in this new directory. It should be a symlink to services from /usr/lib/systemd/system/ that you wish to enable.