M01: Introduction to Operating Systems
TU1: Installing, configuring and exploiting a computer system
ASIX1
Practical Exercise 12: Scheduling Tasks
11-03-21

Practical Exercise 12: Scheduling Tasks

GENERAL CONDITIONS
1- Deadline : 21-03-2020
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_m01tu01pr12.pdf and asix1_surname_name_m01tu01pr12odt
        b2.) DAW1 (English): daw1_surname_name_m01tu01pr12.pdf and daw1_surname_name_m01tu01pr12.odt   
     c)
Subject:
        
c.1) ASIX1 (Engish): asix1_surname_name_m01tu01pr12
         c.2)
DAW1 (English): daw1_surname_name_m01tu01pr12
3- Make this report individually.
4- Left, right, top and bottom margins: 2cm.


DOCUMENTATION ABOUT SCHEDULING TASKS


1- Introduction

a)
Schedule = A list of planned activities or things to be done showing the times and/or dates when they are intended to happen or be done.

b)
It is a good practice to schedule certaing routine tasks to run automatically. For example, as a root  you may want to backup some important folders to a second hard drive or another computer in your local network or even in internet  once a week, you may want to update and upgrade software once a month, scan your system to find spyware, malware or viruses each day, monitor your system performance or disk space each hour,  renew  SSL/TLS certificates of web servers each year, delete files which are no longer required, run maintenance tasks a so on.

c) As a user you may want to perform your own routine tasks as well. For example,  remove files and folder in your Trash bin each day, remove data stored in ~/.cache each month and so on. 


d) Sometimes is a good idea to run commands whenever your system reboots as a root or as normal user. The Linux scheduling system allows root and normal users to run commands whenever the system boots. 

e) In Linux the cron daemon (a daemon is process that runs in the  a background waiting to be activated by the occurance of a specific  event. A server is in fact a type of daemon) executes scheduled commands or tasks periodically at a specific time. These scheduled commands or task are known as "Cron Jobs". 

f) The cron daemon reads the commands it is to execute from crontab files:
    * /etc/crontab is the main system crontab file.
    * Additional system crontab files could be found in /etc/cron.d.
    * Users personals crontab files are stored in /var/spool/cron/crontabs directory. The  files  under  /var/spool/cron/crontabs  are named based on the user's account name
In this practical exercise we will learn how to work with /etc/crontab and users personal crontab files.

g) We can modify the users personal crontab files with the help of a special program called crontab. Be careful, the term crontab has three meanings: It is the name of a program, it is the name of a file stored in /etc and the generic name for any file that cron reads to know the commands it has to execute.


2- Working with /etc/crontab

a)
The typical format of /etc/crontab file is:
    * Each line is a cron job. Any line starting with # is a comment
    * Each line has the following format: minute (0-59)    hour (0-24)    day_of_month (1-31)    month (1-12)    day_of_week (0-7)    user    command
    * minute, hour, day_of_month, month, day_of week, user and command are fields of cron job.
    * Day of week 0 and 7 are Sunday
    * The asterisk operator  means each
minute,  each hour and each month.   The asterisk operator  *  means  "it does not care" in the day_month or day_of_week fields. Generally speaking, * means any value.
    * The comma operator allows you to specify a list of values for repetition. For example 1,3,7 in the hour field means that the task will run at 1am, 3am, and 7am.
    *
The hyphen  -  operator allows you to specify a range of values. If you have 1-4 in the day_of_week field, the task will run from Monday to Thursday.
    * The slash operator  allows you to specify values that will be repeated over a certain interval between them. For example, if you have */10 in the minute field, it means the action will be performed every 10 minutes.

    * The @reboot special string allows you to specify commands that will be run once after reboot.
 
b) Examples:
            # Run the program /sbin/backup.sh with privileges of root user the 31st of each month at 23:00. it does not care if that day is Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday.
            00        23        31        *        *        root        /sbin/backup.sh
           
            # Update and upgrade software
with privileges of root each Sunday at 00:30 each month. It does not care what day of the month is (1,2,3...).
            30        00        *        *        7        root        aptitude update  &&  aptitude upgrade
           

            # Run the program /usr/bin/test.py
with privileges of fje user each minute, each hour, it does not care day of month or week, each month.
            *        *        *        *        *        fje        /usr/bin/test.py
           
            # Run the program /usr/local/bin/empty_cache.sh at 15:00 on every day from Monday through Friday with privileges of fje002 user:
            00        15        *        *        1-5        fje002        /usr/local/bin/empty_cache.sh
           

            # Restart apache2 with root privileges every 5 hours o'clock each day of each month, it does not care what the day oh the month or the week is:
            00        */5        *        *        *        root        systemctl    restart    apache2
         
                       

            # Run the program /etc/cron.daily/disk_alert.sh with privileges of root user the 1st,14th and 28th at 00:00 each month except August. it does not care if that day is Monday, Tuesday,......
            00        01        1,14,28        1-7,9-12        *        root        /etc/cron.daily/disk_alert.sh
 

            # Run the program /etc/cron.daily/disk_alert.sh with privileges of root user the 1st,4th,7th,11th and 14th at 00:00 each month except August. it does not care if that day is Monday, Tuesday,......
            00        01        1-14/3        *       *        root        /etc/cron.daily/disk_alert.sh
     
        
           
           
# Run the program /home/fje/bin/empty_trash.sh with privileges of fje user once after reboot
            @reboot        fje        /home/fje/bin/empty_trash.sh


c)  /etc/crontab can only be edited by root (or any sudo user) usually using nano o geany.
         

3- Working with users personals crontab files

a) The typical format of user personal crontab file is:
    * Each line is a cron job. Any line starting with # is a comment
    * Each line has the following format: minute (0-59)    hour (0-24)    day_of_month (1-31)    month (1-12)    day_of_week (0-7)    command
    *
User field is not required.
    * Operators *  ,  -  /  can be used. Special string @reboot can be used as well.

b) In order to edit the his or her personal crontab file, a user has to run:
        
    EDITOR=nano  crontab  -e

and the nano editor will be run opening the user personal crontab file.

c) In ordrer to display the contents of the user personal crontab file,
a user has to run:

    crontab  -l

4- Checking system date and time

a)
Show date in human format:   date +"%d-%m-%y"
b)
Show time in human format:   date +"%H:%M:%S"
c)
Show date and time in computer backup and log format: date +"20%y%m%d%H%M%S"

5
- Additional documentation
a) https://www.ostechnix.com/a-beginners-guide-to-cron-jobs/
b) https://linuxize.com/post/scheduling-cron-jobs-with-crontab/
c) https://www.configserverfirewall.com/linux-tutorials/linux-run-cron-job-reboot/


PRACTICAL EXERCISE


PART 1

1-
Shutdown your system. Create a new SATA hard drive with the following characteristics:
    a) Hard drive type: VDI
    b) Dinamically allocated
    c) Name: disk03
    d) Size: 30GB
    e) Attached to SATA 3

2- Check and mark clearly the new hard drive identification with the help of lsscsi.
Take a screenshot.

3.-
Run  fdisk on /dev/sdd and:

    a) Create a new  MBR (DOS) partition table on the selected disk.
    b) Create a single primary / Full size / Linux partition
    c) Show the /dev/sdd partition table. Take a screenshot.

4- Install (build) an ext4 filesystem on /dev/sdd1. Take a screenshot.

5- Mount /dev/sdd1 on a directory called /mnt/backups. Create the directory if it does not exist.  Check the directory permissions and 
a detailed report of /dev/sdd disk space usage. Take a screenshots.

6- Change /etc/fstab in order to mount /dev/sdd1 on a folder called /mnt/backups automatically when your computer boots. At the end of /etc/fstab add the following entry:

/dev/sdd1    /mnt/backup    ext4    defaults    0    0

Take a screenshot of /etc/fstab.

7-
Create the following bash script:
#!/bin/bash
echo "Starting the backup process...."
if [[ ! -d /mnt/backups/log ]]
then
    mkdir /mnt/backups/log
    touch
/mnt/backups/log/backup.log
    chmod -R 700
/mnt/backups/log
fi
DATE_FORM1=$(date +20%y%m%d%H%M%S)
DATE_FORM2=$(date
+"%d-%m-%y %H%M%S")
tar cf /mnt/backups/backup_home_$DATE_FORM1.tar /home 2> /dev/null
gzip -f /mnt/backups/
backup_home_$DATE_FORM1.tar
if [[ $? == 0 ]]
then
    echo "$DATE_FORM2: Backup process finished successfully" >>
/mnt/backups/log/backup.log
    echo "Finishing the backup process.."
else
   
echo "$DATE_FORM2: Backup process was not successfully finished"  >> /mnt/backups/log/backup.log
    echo "Finishing with some errors the backup process..."
fi
exit 0
   

Save the script in a file with the following name: backup.sh. With root privileges move backup.sh into the /usr/sbin directory. Set root of as the owner of backup.sh. Set root as the group of backup.sh. Add the execution permission only to the root user. Remove any permission for the group and others. Take a screenshot showing that backup.sh was properly created and installed.
8- Check system date and time in human format. Take a screenshoot.
9- Add a system cron job to run backup.sh with root privileges in 5 minutes.
Take a screenshot of /etc/crontab.
10- Add a system cron job to run backup.sh with root privileges in 10 minutes. Take a screenshot of /etc/crontab.
11- Add a system cron job to run backup.sh with root privileges each day at 00:00. Take a screenshot of /etc/crontab.
12-
Restat the cron service.
Take a screenshot
13- Check that  two backups of /home  have been created into /mnt/backups. Take a screenshot.
14- Check that  two new entry have been added to backup.log. Take a screenshot. 

PART 2


1- Install mate-tweak. Select System --> Preferences --> Look and Feel --> MATE Tweak --> Desktop. Enable Computer and Trash icons on your Desktop.
2- Create a folder called bin in the personal folder of your default user.
3-
Create the following bash script:
   
#!/bin/bash
    echo "Emptying Trash bin...."
    sleep 10
    rm -rf ~/.local/share/Trash/files/*
   
if [[ $? == 0 ]]
    then
        echo "Trash bin successfully emptied"
    fi
    exit 0

Save the script in a file with the following name: empty_trash.sh. Move empty_trash.sh into the ~/bin directory. Add the execution permission only to the your default user. Remove any permission for the group and others.
Take a screenshot showing that empty_trash.sh was properly created.
4- Add a personal cron job to run empty_trash.sh every 5 minutes.
Take an screenshot of your personal crontab
5
- Run  watch  -n  1  'ps -aux  |  tail  -n 10' and take an screenshot the moment empty_trash.sh is automatically run by the system.

6- Check that the Trash bin has been emptied.  Take an screenshot.


PART 3


1-
Create the following bash script:
    #!/bin/bash
    if [[ ! -e ~/counter.log.html ]]
    then
        echo "It is your log in the system number 1" > ~/counter.log.html
   
else
        count=$(cat ~/counter.log.html | cut -d " " -f 9)
        count=$(expr $count + 1)
       
echo "It is your log in the system number $count" > ~/counter.log.html   
    fi
    #firefox ~/counter.log.html
   
exit 0
Save the script in a file with the following name: counter.sh. Move counter.sh into the ~/bin directory. Add the execution permission only to the your default user. Remove any permission for the group and others.
Take a screenshot showing that counter.sh was properly created.
2
- Add a personal cron job to run counter.sh once after the reboot.
Take an screenshot of your personal crontab.
3-
  Reboot your system two times with your teacher checking if everything works fine.
Take an screenshot of the web page shown by firefox the after the second reboot of your system.