EH1: Linux Lab
ASIX/DAW/DAM-1
Task A5: Scheduling tasks. Cron service. crontab
4-4-24

Task A5: Scheduling tasks. Cron service. crontab

GENERAL CONDITIONS
1- Deadline : 21-04-2024
2- Send your report 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_eh1act05.pdf and asix1_surname_name_eh1act05.odt
        b2.)
DAW1 (English): daw1_surname_name_
eh1act05.pdf and daw1_surname_name_eh1act05.odt   
     c)
Subject:
        
c.1)
ASIX1 (Engish): asix1_surname_name_
eh1act05
         c.2)
DAW1 (English): daw1_surname_name_eh1act05
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

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/bin/test.py with privileges of fje user every 10 minutes, each hour, it does not care day of month or week, each month.
            */10        *        *        *        *        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. Remove any hard drive with the only exception of  debian12.1.vdi.


2- Create a new SATA hard drive with the following characteristics:
    a) Hard drive type: VDI
    b) Dinamically allocated
    c) Name: eh1act04
    d) Size: 30GB
    e) Attached to SATA Port 1


3- Run GParted. Select the newly created hard drive and:

    a) Create a new  MBR (MSDOS) partition table on the hard drive.
    b) Create a one only Ext4 primary partition using
the hard drive full size.
    c) Take a screenshot


4- Mount the new partition and protect the mount point:

5-
M
ake the partition available to any member of group disk

6-
Make the new partition available to your user:

7-
Mount the new partition
automatically when your computer boots:

8-
Reboot your system and check:


PART 2

0-
Disable StUpdSw.service that your created in activity eh1act03.

1-
Create a folder called scripts on your personal folder. Afterwards, create in scripts a bash script
to create a full complete backup of your home folder on /media/eh1act04:
#!/bin/bash
echo "Starting the backup process...."
if [[ ! -d /media/eh1act04/log ]]
then
    mkdir /media/eh1act04/log
    touch
/media/eh1act04/log/backup.log
    chmod -R 700
/media/eh1act04/log
fi
DATE_FORM1=$(date +20%y%m%d%H%M%S)
DATE_FORM2=$(date
+"%d-%m-%y %H%M%S")
tar cf /media/eh1act04/backup_home_$DATE_FORM1.tar /home 2> /dev/null
gzip -f /media/eh1act04/
backup_home_$DATE_FORM1.tar
if [[ $? == 0 ]]
then
    echo "$DATE_FORM2: Backup process finished successfully" >>
/media/eh1act04/log/backup.log
    echo "Finishing the backup process.."
else
   
echo "$DATE_FORM2: Backup process was not successfully finished"  >> /media/eh1act04/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.

2- Check date and time.

3-
Add a system cron job to run backup.sh with root privileges every 3 minutes.
Take a screenshot of /etc/crontab.

4-
 
Show contents (size, permissions, ownership, date..) of /media/eh1act04 in 10 minutes. Check that every 3 minutes a backup of your home directory of /home  has been created  into /media/eh1act04. Take a screenshot

5-
Check that   3 new entries have been added to backup.log. Take a screenshot.
 




PART 3

1- Check date and time.

2- Comment the system  cron job created in PART 2

3-
Add a system cron job to run backup.sh with root privileges in 5 minutes.
Take a screenshot of /etc/crontab.

4-
 
Show contents (size, permissions, ownership, date..) of /media/eh1act04 in 5 minutes. Check that  a backup of your home directory of /home  was created  into /media/eh1act04 at the time scheduled. Take a screenshot

5-
Check that   a new entry was added to backup.log. Take a screenshot.
 




PART 4


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. Change to the new folder.

3-
Create the following bash script that removes contents of your Trash bin:
#!/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 with the proper permissions.

4
- Add a personal cron job to run empty_trash.sh every 2 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.  Show contents of Take an screenshot



PART 4


1-
Comment
the personal cron job created in PART 3

2-
Create the following bash script that counts times you log on the system with your personal user account:

#!/bin/bash
if [[ ! -e ~/counter.log ]]
then
   echo "It is your log in the system number 1" > ~/counter.log
else
   count=$(cat ~/counter.log | cut -d " " -f 9)
   count=$(expr $count + 1)
  
echo "It is your log in the system number $count" > ~/counter.log   
fi
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.

3
- Add a personal cron job to run counter.sh once after the reboot.
Take an screenshot of your personal crontab.

4-
  Reboot your system three times. Afterwards, 
Show: