Bookmark this page

Guided Exercise: Schedule Recurring System Jobs

In this exercise, you schedule commands to run on various schedules by adding configuration files to the system crontab directories.

Outcomes

  • Schedule a recurring system job to count the number of active users.

  • Update the systemd timer unit that gathers system activity data.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available.

[student@workstation ~]$ lab start scheduling-system

Instructions

  1. Log in to the servera machine as the student user and switch to the root user.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$ sudo -i
    [sudo] password for student: student
    [root@servera ~]#
  2. Schedule a recurring system job that generates a log message to indicate the number of active users in the system. This job must run daily and use the w -h | wc -l command to retrieve the number of active users in the system. Use the logger command to generate the log message of currently active users.

    1. Create the /etc/cron.daily/usercount script file with the following content:

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

      [root@servera ~]# chmod +x /etc/cron.daily/usercount
  3. Install the sysstat package. The timer unit must trigger the service unit every ten minutes to collect system activity data with the /usr/lib64/sa/sa1 shell script. Change the timer unit configuration file to collect the system activity data every two minutes.

    1. Install the sysstat package.

      [root@servera ~]# dnf install sysstat
      ...output omitted...
      Is this ok [y/N]: y
      ...output omitted...
      Complete!
    2. Copy the /usr/lib/systemd/system/sysstat-collect.timer file to the /etc/systemd/system/sysstat-collect.timer file.

      [root@servera ~]# cp /usr/lib/systemd/system/sysstat-collect.timer \
      /etc/systemd/system/sysstat-collect.timer
    3. Edit the /etc/systemd/system/sysstat-collect.timer file for the timer unit to run every two minutes. Replace any occurrence of the 10 minutes string with 2 minutes throughout the unit configuration file, including the occurrences in the commented lines. Use the vim /etc/systemd/system/sysstat-collect.timer command to edit the configuration file.

      From these changes, the sysstat-collect.timer unit triggers the sysstat-collect.service unit every two minutes, and collects the system activity data in a binary file in the /var/log/sa directory.

      ...output omitted...
      # Activates activity collector every 2 minutes
      
      [Unit]
      Description=Run system activity accounting tool every 2 minutes
      
      [Timer]
      OnCalendar=*:00/2
      
      [Install]
      WantedBy=sysstat.service
    4. Notify the systemd daemon of the changes.

      [root@servera ~]# systemctl daemon-reload
    5. Activate the sysstat-collect.timer unit.

      [root@servera ~]# systemctl enable --now sysstat-collect.timer
      ...output omitted...
    6. Wait until the binary file is created in the /var/log/sa directory.

      The while command, ls /var/log/sa | wc -l returns 0 when the file does not exist, or returns 1 when the file exists. The while command pauses for one second when the file is not present. The while loop exits when the file is present.

      [root@servera ~]# while [ $(ls /var/log/sa | wc -l) -eq 0 ];  \
      do sleep 1s; done
    7. Verify that the binary file in the /var/log/sa directory was modified within two minutes.

      [root@servera ~]# ls -l /var/log/sa
      total 4
      -rw-r--r--. 1 root root 2540 Apr  5 04:08 sa05
      [root@servera ~]# date
      Tue Apr  5 04:08:29 AM EDT 2022
    8. Return to the workstation machine as the student user.

      [root@servera ~]# exit
      logout
      [student@servera ~]$ exit
      logout
      Connection to servera closed.
      [student@workstation ~]$

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish scheduling-system

This concludes the section.

Revision: rh199-9.0-4fecb06