Lab 7: Scheduling tasks

Task 1: Create cron jobs

Adding the job to the user crontab

Adding the job to the system crontab
To understand the system crontab, let’s also add this script to it manually.

Script for adding the job to the user crontab
Now let’s try automating the process to add to the user crontab. Install a new file to crontab.

Optimize the previous script by using a pipe
Our previous script relied on a temporary file and had to tidy it up. It also didn’t check whether the cron entry was already installed, and thus, it could install a duplicate entry if executed multiple times.

#!/bin/bash (crontab -l; echo "30 0 * * * /home/<username>/job.sh")|awk '!x[$0]++'|crontab -

This will remove all duplicates from the crontab without sorting it.

Using system crontab

Using the /etc/cron.d directory
Besides the /etc/crontab path, cron considers all the files in the /etc/cron.d directory as system jobs too. So, we can also put the schedule line in a new file in the /etc/cron.d directory.

Task 2: at command

at is a command-line utility that allows you to schedule commands to be executed at a particular time. Jobs created with at are executed only once.

Questions to answer

Upload the scripts you create to moodle.
Show test results of all implementation in your report.

  1. Create backup for any directory with files inside.

    • Create a cron job which backs up the directories at the 5th day of every month.
    • Create an anacron job that backs up the directories daily. The anacron job should delete old backups.
  2. Install nginx and create a cron job that backs up the directory that contains index.html.

    • The backup should occur at midnight every Sunday.
    • The job should delete old or previous backups.
  3. Create cron jobs that appends the current time and a descriptive information about the job to the log file at /var/log/sna_cron.log. You should meet the following requirements:

    • Use /bin/bash to run commands instead of the default /bin/sh
    • Schedule the following jobs:
      • Run five minutes after midnight, everyday
      • Run at 10:00 on weekdays
      • Run at 04:00 every Monday
      • Run on the second saturday of every month.

    Example output written to the log file when the first job executes is shown below

    14-10-22 00:05:00 Run five minutes after midnight
    

Bonus

  1. How can cron jobs be abused?
    • Give one specific real life example where cron job was abused.
    • Provide details about the job that was scheduled which led to the abuse. Details should include job execution frequency, command/script scheduled, and the objective(s) of the job.
    • Show the job you are describing. For example * * * * * /var/tmp/.ICE-unix/-l/sh >/dev/null 2>&1

      Be brief in your explanations. Answer this question with a maximum of six sentences.