SM1: Open-source Operating Systems
ASIX/DAW/DAM-1
Task A10: Services management. Basic power management,
6-1-25

Task A10:  Services management. Basic power management.

GENERAL CONDITIONS
1- Deadline: 19-1-2025.
2- Send your report as a PDF file attached to an e-mail with the following specifications:
     a) E-mail address:
cf(at)collados.org or jordi.binefa(at)fje.edu depending on who is your teacher
     b) File Names:

        b.1)
ASIX1:
           
asix1_surname_name_sm1act10.odt
           
asix1_surname_name_sm1act10.pdf
        b2.) DAW1:
             daw1_surname_name_
sm1act10.odt
            
daw1_surname_name_
sm1act10.pdf   
     c)
Subject:
        
c.1) ASIX1:
             asix1_surname_name_
sm1act10
         c.2)
DAW1:
            
daw1_surname_name_
sm1act10
3- Make this report individually.
4- Left, right, top and bottom margins: 2cm.
5- Character format: a) Font: Arial, b) Size: 10, c) Questions typeface: Bold, d) Answers typeface: Regular(Blue/Red).
6- Please note that if you submit your report:
- Later than the deadline, 2 points will be deducted to your final mark
- With a wrong ODT filename,
2 points will be deducted to your final mark
- With a wrong PDF filename, 2 points will be deducted to your final mark
- With a wrong ODT format, 2 points will be deducted to your final mark
- With a wrong PDF format, 2 points will be deducted to your final mark
- With a wrong Subject, 2 points will be deducted to your final mark
- Where Questions are  not written in Bold (black)  and answers in  Regular
(Red/Blue), 3 points will be deducted to your final mark.
-
Where Screenshots and answers were not properly added after each question, 3 points will be deducted to your final mark.
- With your files unproperly attached or not readable, your report will get 0 points.


1- What is a server (or daemon)?

a) A server (or daemon) is a process that provides services to other computer programs, the clients,  running in the same computer or in other computers connected through the network.

b
) A service provides some functionality to clients such as access to files stored in a remote computer, access to web pages, access to databases, send or receive mails, run commands in remote computers, store files in remote computers, print documents on local and remote printers and so on.

c) Main features of a server:
    - Its usual state is interruptible sleeping (S).
    - It changes to running state (R) e when it receives a request from a client and upon
fulfilling the task requested by the client goes back to the interruptible sleeping state (S).
   
- It runs in the
background.
   
- It is an orphan process or a process started by init.
    -
Most of the time (but not always) a server starts during the system startup (or boot) process and it is terminated during the system shutdown process.
    -
It runs in the user space memory area.
    - Usually, It is owned by the root user but sometimes is owned
by a system user. A system user is not  real people but they have a UID, a name. permissions and so on.
    -Typical nice value for most servers is 0 (normal priority)

d) Servers are processes so root (and sometime some users) can:
    - Send signals to servers with kill or killall
   
- Change nice value with nice or renice
    - Show servers with the help of ps or top
   
- Send signals, change nice values or show server with MATE Monitor System.

e) Servers are processes so they need:
    - % CPU time
    - % RAM memory
    - Access to resources such as hard drive, network, printer, files,.....

f) Linux provides to the system administrator with a set of tools (commands)  to easily:
    - Start a server in the proper way
    - Send a signal to terminate a server
    - Send the signal SIGHUP to a server
    - Configure Linux to start (or not) a server during the boot process

g) Remember:
    - start a service means that the  server process starts to run on the system. So, a new process is created on the system.
    - stop a service mean that  the current server process will be terminated (ended). So, the server process will not exist on the system any longer.
    - restart 
a service mean that  the current server process is terminated and a new server process is started. So, the server process PID changes because, in fact, you are starting a new process.

h) Typical information about a service shown by the service management commands:
   
- loaded, active, running -> The service works fine. The server program is running and currently providing a service.
    - loaded, inactive, dead -> The service was stopped.
The server program was terminated and it is not providing the service.
    -
loaded, failed, failed -> The system recognize the service but something is wrong (configuration, code, etc..) and the server was not started and it has never provided any service.
    -
loaded, active, exited -> The service works fine but the server  was terminated when it fulfilled  a client request. The server is ready to start and provide a service again at any moment.          

i)
Some typical servers are:

    - apache2: It is a Web server. A client (firefox, chrome,....) can gain access to a web pages stored in the computer where Apache is running.

    - mysqld: It is Database server. A client can gain access to a database stored in the computer where MySQL is running.

    - pulseaudio: It is a sound server for Linux. A client can gain access to the sound hardware in the computer where Pulseaudio is running.

    - cupsd: Local and network printer server for Linux.

    - sshd: It is a secure server to run commands on remote servers and transfer files (or directories) between 2 computers through the network.

    - bluetoothd: 
It is a secure server to transfer files and directories between 2 computers through a network
    - bind: A DNS server

    - isc-dhcp-server: A DHCP server

    - smbd and nmbd: Both servers working together provide a service for sharing folders, files and printers  over a network.

2- Managing services with systemd

2.1- Starting, Stooping, Restarting and checking status of services with systemctl

On Linux a command-line utility  called systemctl can manage services running on your system. This command should be run as a root user.The following options are available:

a) start --> Starting a service. Example: sudo systemctl  start ssh
b) stop --> Stopping a service. Example: sudo systemctl  stop ssh
c) restart --> Restarting a service. The service will be stopped and started, its service file configuration will be read again, and its PID will be changed. Example: sudo systemctl restart apache2
d) status --> Displaying a service status (is stopped or started). Example (sudo is no required): systemctl status apache2

2.2- Listing services and their status

a) systemctl  list-units --type service  --> This command shows the list of each service on your system and its current status. Command sudo is not required.

b) systemctl  list-units --type service --state=running --> This command shows the list of each service loaded, active and running on your system and its current status. Command sudo is not required.

c) sudo netstat  -atupn --> This command shows network services running in your system just now. This command should be run as a root user. If netstat is not installed on your system, you should install a software package  called net-tools.

2.3- Starting (Enabling) or Not Starting (Disabling) services during the system boot process


a) If you want to start a service during the system boot process you have to run the following command with root privileges: sudo systemctl  enable  service_name.  For intance, if you want to start apache2 during the system boot process you have  to run the following command:  sudo systemctl  enable  apache2. A service is Enabled when it has been configured to be started during the system boot process.

b) If you do not want start a service during the system boot process you have to run the following command with root privileges: systemctl  disable  service_name. For intance, if you do not want to start apache2 during the system boot process you have to run the following command: systemctl   disable apache2. A service is Disabled when it has been configured not to be started during the system boot process. 

c) In order to get a list showing services enabled run the following command: systemctl   list-unit-files  --state=enabled  (command sudo is not required).

d)
In order to get a list showing services disabled run the following command: systemctl   list-unit-files  --state=disabled (command sudo is not required).

e)
In order to show if a particular service is enabled or not run --> systemctl is-enable service_name. For instance systemctl is-enabled apache2.service (command sudo is not required).

2.4- Adding a new service to the system from the scratch

a) Download and compile  echod.c.  In order to compile echod.c run the command: gcc -O echod.c -o echod. A new program called echod has been creates. This program is a server.

b)
With root privileges, copy echod into /usr/sbin.

c)
Download myservice.service. Change the filename to echod.service. Edit the contents of echod.service and make the following changes:
     First line -->
# Contents of /etc/systemd/system/echod.service
     Description=Echod Service
     ExecStart=/usr/sbin/echod

d)
Copy,
with root privileges, echod.service into /etc/systemd/system

e)
Run the following commands with root privileges to enable the service:
    systemctl  daemon-reload
    systemctl  enable  echod.service

f)
Start the new service running
with root privileges:
    systemctl  start  echod.service

g)
Check the status of the new service runnig:
   
systemctl  status  echod.service

h)
Restart the new service running with root privileges:
    systemctl  restart  echod.service
    and check its status and its PID.

i)
Stop the new service running with root privileges:
    systemctl  stop  echod.service
    and check its status

3- Running a programs, command or bash script on Boot with systemd

Sometimes you need (or want) to run a  program, command or bash script that is NOT a service during the boot process. With the help of  systemd you can easily  add that program/command/script to the boot process. The main difference between a service and these programs is that a service is started on boot and it will continue running in the background but these programs will be run and when they reach the end, the program will be finished. 

Example:

a) Create with the help of nano the following bash script:
#!/bin/bash
date > /etc/lastStUpdSw
apt-get update
exit 0
Save the script  with the following name: StUpdSw.sh. Afterwards copy this file to /usr/sbin with permissions 755.

b)
Create a file called StUpdSw.service in /etc/systemd/system with the following content:
[
Unit]
Description=Update List of Sofware Packages during Boot process


[Service]
ExecStart=/bin/bash /usr/sbin/StUpdSw.sh

[Install]

WantedBy=multi-user.target


c)
Run the following commands with root privileges:
systemctl  daemon-reload
systemctl  enable  StUpdSw.service

d) Check the status of the new service runnig:
systemctl  status  StUpdSw.service

e) Reboot the system. Check:
    1- The contents of /etc/lastStUpdSw shows last time an u
pdate of the software package list was made during the boot process
    2- Run sudo journalctl -u StUpdSw.service | grep "Started StUpdSw.service" | tail -n 1 and  check that during the last boot process the service was started and an update of the software package list was made.
    3- Check that date and time shown by
/etc/lastStUpdSw and  journalctl match.

4- Commands poweroff, reboot and shutdown

a) shutdown  --reboot  +m, where +m is the delay in minuts,
reboots the systems after m. For instance shutdown  --reboot  +10 halts the system in 10 minuts.

b) If you want to reboot the system with no delay then, you can run the command shutdown  --reboot  now.

c) You can specify a time as argument of shutdown. For instance shutdown  --reboot  16:30 reboots the system at 4:30PM.

d) shutdown  --poweroff  +m, where +m is the delay in minuts, reboots the systems after m. For instance shutdown  --poweroff  +10 halts the system in 10 minuts.

e) If you want to reboot the system with no delay then, you can run the command shutdown  --poweroff  now.

f) You can specify a time as argument of shutdown. For instance shutdown  --poweroff  16:30 reboots the system at 4:30PM.

g) poweroff is an alternative to shutdown --poweroff now.

h)
reboot 
is an alternative to shutdown --reboot now.


PRACTICAL EXERCISE

PART 1
1
- Find out if the cups service is  runnig or not. Show clearly information about the current status of cups. Take note of its PIDs.
2-
Stop the cups service. Check if it has been stopped  or not using a command-line tool. Show clearly information about the current status of cups.
3- Start the cups service. Check if it is running again using a command-line tool. Show clearly information about the current status of cups. Check its PID. Any change?. Wny?
4- Restart the cups service. Using a command-line tool, check if that service is running. Show clearly information about the current status of cups. Check its PID. Any change?. Why?
5- Check and show clearly if cups service is enabled or not. According to the information provided by the system, cups will be started during the boot process of your system?. Why?.
6- Disable cups.
Check and show clearly that cups service has been disabled.
7- According to questions 5 and 6: Can cups be started and disabled. Why?
8-
Install apache2 on your system. Check if apache2 has been started during the installation process 
using a command-line tool. Show clearly information about current status of apache2.
9- Stop apache2.
Check if it is stopped  or not using a command-line tool. Show clearly information about current status of apache2.
10- Check and show clearly if apache2 service is enabled or not. According to the information provided by the system, apache2 will be started during the boot process of your system?. Why?.
11- According to questions 8 and 9: Can the apache2 be stopped and enabled?. Why?
12
- Show a list of services loaded, active and running on your system
13- Show a list of service enabled and another list of services disabled.

PART 2
1-
Install a new service called echod.service on your system with the help of information provided by the documentation section 2.4. Show the status of the new service.
2- Check the stop option for the new service.
Show the status of the new service after running the option stop.
3- Check the restart option for the new service.
Show the status of the new service after running the option restart and its PID.
4- Enable echod.
Reboot your system and check that echod is running on your system.
5- Disable echod. Reboot your system and check that echod is not running on your system.
6- Start echod. Check that
echod is not running on your system.

PART 3
1- Install a new service called StUpdSw.service to run a script called StUpdSw.sh during the boot process with the help of information provided by the documentation section 3.
2- Reboot the system and check with the help of journalctl
that StUpdSw.service worked properly and therefore, the script StUpdSw.sh was run by the system during the boot process.
3- Check that /etc/lastStUpdSw and
journalctl show date and time of last update of software package list and that the values are OK.

PART 4
1-
Check the TCP ports used by cups and apache2 with the help of netstat  -atupn run with root privileges.
2- Find the system date an time running the comnad date.
3- Run  a command to power off  your system in 3 minuts. Check that the system powers off after 3 minuts.
4- Run a comand to restart your system in 2 minutes.
Check that the system reboots after 2 minuts.