Bookmark this page

Guided Exercise: Scheduling System cron Jobs

In this lab, you will work with recurring system jobs.

Resources
Files:
  • /etc/crontab

  • /etc/cron.d/*

  • /etc/cron.{hourly,daily,weekly,monthly}/*

Machines: desktopX

Outcomes

A daily job to count the number of active users, and an updated cron job to gather system performance data.

  1. Log into your desktopX system as student, then elevate your privileges to root.

    1. [student@desktopX ~]$ su -
      Password: redhat
  2. Create a new daily cron job that logs a message to the system log with the number of currently active users (w -h | wc -l). You can use the logger command to send messages to the system log.

    1. Open a new file in /etc/cron.daily in an editor, e.g., /etc/cron.daily/usercount.

      [root@desktopX ~]# vim /etc/cron.daily/usercount
    2. Write the script that logs the number of active users to the system log.

      Insert the following in your editor:

      #!/bin/bash
        USERCOUNT=$(w -h | wc -l)
        logger "There are currently ${USERCOUNT} active users"
    3. Make the script executable:

      [root@desktopX ~]# chmod +x /etc/cron.daily/usercount
  3. The sysstat package, when installed, has a cron job that runs every 10 minutes, collecting data using a command called sa1. Make sure this package is installed, then change this job to run every five minutes.

    1. Make sure the sysstat package is installed.

      [root@desktopX ~]# yum -y install sysstat
    2. Find out in which file the sysstat package has configured the cron jobs. Cron jobs are generally configured in files marked as a configuration file for the package manager.

      [root@desktopX ~]# rpm -qc sysstat

      /etc/cron.d/sysstat looks promising.

    3. Open /etc/cron.d/sysstat in an editor.

      [root@desktopX ~]# vim /etc/cron.d/sysstat
    4. Change */10 on the sa1 line to */5.

    5. Save your changes and exit.

    6. Monitor the files in /var/log/sa to see when their sizes and timestamps change.

      [root@desktopX ~]# watch ls -l /var/log/sa
Revision: rh199-7-d0984a3