EH1: Linux Lab
ASIX/DAW/DAM-1
Task A3: Managing services. Basic power management
11-3-24

Task A3:  Managing services. Basic power management.

GENERAL CONDITIONS
1- Deadline: 24-3-2024.
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_eh1act03.pdf and asix1_surname_name_eh1act03.odt
        b2.) DAW1 (English): daw1_surname_name_
eh1act03.pdf and daw1_surname_name_eh1act03.odt   
     c)
Subject:
        
c.1) ASIX1 (Engish):  asix1_surname_name_
eh1act03
         c.2)
DAW1 (English): daw1_surname_name_
eh1act03
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) 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 somqething is wrong (configuration, code, etc..) and the server was not started and it has never provided any service.
        -
loaded, active, exited -> The service configuration is OK but the server was started, it provided a service and was terminated. The server is ready to start and provide a service at any moment.          

n)
Some typical servers are:q

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
aptitude update
date > /etc/lastStUpdSw

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 and  check that during the 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 are OK.

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-
Stop apache2.
Check if it is stopped  or not using a command-line tool. Show clearly information about current status of apache2.
9- 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?.
10- According to questions 8 and 9: Can the apache2 be stopped and enabled?. Why?
11
- Show a list of services loaded, active and running on your system
12- 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.