M01: Introduction to Operating Systems
TU1: Installing, configuring and exploiting a computer system
ASIX1
Practical Exercise 8: Managing services. Basic power management. 14-02-23

Practical Exercise 8:  Managing services. Basic power management.

GENERAL CONDITIONS
1- Deadline: 26-2-2023.
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 Name:

        b.1)
ASIX1 (English): asix1_surname_name_m01tu01pr08.pdf and asix1_surname_name_m01tu01pr08.odt
        b2.) DAW1 (English): daw1_surname_name_m01tu01pr08.pdf and daw1_surname_name_m01tu01pr08.odt   
     c)
Subject:
        
c.1) ASIX1 (Engish): asix1_surname_name_m01tu01pr08
         c.2)
DAW1 (English): daw1_surname_name_m01tu01pr08
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

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) The usual state of  a server is interruptible sleeping (S) because is a program that awaits a request from a client program. When a request is received from a client, the server program wakes up, fulfill the task requested by the client (so it change to the R state) and goes back to the interruptible sleeping state (S).

d)
A server is a process that is run in the
background.That means:
    *
A background process works independently of the shell, leaving the terminal free for other work.
    * A background process  is not attached to your keyboard and screen and it runs without user intervention.
    * Users are not aware of the fact that
background processes  run on their systems altough  these processes are extremely important to the normal working of an operating system.
    * Users (and root) do not interact directly with a background process but a signal can be sent to a background process by users (or root). 

f) Usually a server is an orphan process or a process started by init and therefore, a server parent process is usually init.

g)
The most of the times a server starts during the system startup (or boot) process and it is terminated during the system shutdown process. If you want to start a server when the system is running then you need special command to start the server in the proper way. 
If you want to terminate a server when the system is running then you need another special command to terminate the server in the proper way.    

h)
A server runs in the user space memory area
.

i) Servers are owned:
    * Usually by the root user and their default group is root.
    * Sometimes, servers can be owned by specials users called system users. System users are not real people but they have a UID, a name. permissions and so on.

j) Typical nice value for most servers is 0 (normal priority)

k) 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.

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

l) 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

m) Information about a service shown by typical service management commands:
    * LOAD:
Whether or not the service configuration was loaded in memory (LOAD). Typical values:
         - loaded (configuration accepted and stored in memory)
         - masked (prevents activation until unmasked
         - not-found
         - bad-setting (configuration errors)
         - error
(configuration errors)
    * ACTIVE: It shows the high-level service state
. Typicial values: active, reloading, inactive, failed, activating, deactivating
        - active  --> Generally speaking, it means that service has started sucessfully
        - inactive -->
Generally speaking, it means that service is stopped.
        - failed
--> Service was not started because it failed in some way.
    * SUB (short for substates):
         - It shows the low-level service state.
Different set of typical values depending on the service. 
         -
Typicial values: running, failed, exited, dead, listeninig, plugged,mounted.....
        -  running --> 
It means that there is currently a process associated to the service.
        -  failed
--> Service was not started because it failed in some way.
         - dead --> Service was stopped and therefore, the process assciated to the service was terminated
        - exited -> 
Service was started and stopped. Some service are started, provide a service and are stopped
    Typical combinations:
        - loaded, active, running -> The services works fine. The server program is running and providing the service.
        - loaded, inactive, dead -> The service was stopped.
The server program was terminated and it is not providing the service.
        -
loaded, failed, failed -> The service was not started
        -
loaded, active, exited -> The server was started, it provided a servcice and was terminated. The server is ready to start and provide a service at any moment.  

n)
Some typical servers are:

2- Managing services with systemd

2.1- systemctl
A command-line utility that can report or change the status of services. This command should be run as a root user.The following options are available:
a) start --> Starting a service. Example: systemctl  start ssh
b) stop --> Stopping a service. Example: 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: systemctl restart apache2
d) status --> Displaying a service status (is stopped or started). Example: systemctl status apache2

2.2- service
Anothe command-line utility that can report or change the status of services. This command should be run as a root user.The following options are available:
a) start --> Starting a service. Example: service ssh start
b) stop --> Stopping a service. Example: service ssh stop
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: systemctlservice apache2 restart
d) status --> Displaying a service status (is stopped or started). Example: service apache2 status

2.3- Listing services in your system
a) systemctl  list-units --type service --> This command shows:

b) 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.4- Adding o removing a service during the system boot process
a) If you want to totally remove a service during the system boot process you have to run the following command:

systemctl  disable  service_name

For intance, if you want to remove apache2 during the system boot process you have to run the following command:

systemctl   disable apache2

b) If you want to add  a service during the system boot process you have to run the following command:

systemctl  enable  service_name

For intance, if you want to add apache2 during the system boot process you have to run the following command:

systemctl  enable  apache2

c) In order to get a list showing services enabled and disabled, run the following command:

systemctl   list-unit-files  --type=service

NOTE: The meanings of static and masked are beyond the scope of the prsent exercise.

2.5- 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)
As a  root user, 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 echod.service into /etc/systemd/system
e)
Run the following commands with root privileges:
    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- 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.

4- Suspending and hibernating the system

a) Storing a system states means:


b)
To suspend (or suspend-to-RAM)  the system, type the following command at a shell prompt as root user :

systemctl  suspend

Features of this state:
 NOTE: After suspending your system, on VirtualBox click on Machine --> ACPI Shutdown in order to wake up your system.


c)
To hibernate (or suspend-to-disk)  the system, type the following command at a shell prompt as root user:

systemctl  hibernate


Features of this state:
NOTE: After hibernating your system, you have to boot again your computer, but it boots quickly and the system state is restored.

PRACTICAL EXERCISE

1-  Install exim4 (a mail server) running:  aptitude install exim4. Do not take screenshots.
2- Find out the following features of  apache2, ssh, exim4 and cups, using the MATE System Monitor
: a) PID, b) owner, c) status and d) command-line.
3- Find out if the cups service is  runnig or not. Take note of its PIDs.
4-
Terminate the cups service. Check if it has been stopped  or not using a command-line tool.
5- Start the cups service. Check if it is running again using a command-line tool. Check its PID. Any change?.
6- Restart the cups service. Using a command-line tool, check if that service is running. Check its PID. Any change?.
7- Stop apache2.
Check if it has been stopped  or not using a command-line tool. 
8
- Show services loaded and active running on your system
9- Show a list of service enabled during the computer boot process.
10- Remove apache2, ssh and exim4 as services started by the computer during the boot process. Reboot your system and check that both services are not running.
11- Add apache2, ssh and exim4 as services started by the computer during the boot process. Reboot your system and check that both services are running.
12- Install a new service called echod on your system according to information provided by the documentation section 2.5. Show the status of the new service.
13- Check the stop option for the new service.
Show the status of the new service after running the option stop.
14- Check the restart option for the new service.
Show the status of the new service after running the option restart and its PID.
15- Enable echod.
Reboot your system and check that echod is running on your system.
16- Disable echod. Reboot your system and check that echod is not running on your system.
17- Start echod. Check that
echod is not running on your system.
18- Check the TCP ports used by exim4, ssh, cups and apache2 with the help of netstat  -atupn run with root privileges.
19- Find the system date an time running the comnad date.
20- Run  a command to power off  your system in 3 minuts. Check that the system powers off after 3 minuts.
21- Check, in seconds (and write), the difference between starting your system from poweroff  and suspend states.
22- Run command free -m. Check the amount of RAM and SWAP space on your system.  Can you hibernate your system?. Why?.