RHCSA Rapid Track
In this exercise, you will schedule commands to run on various schedules by adding configuration files to the system crontab directories.
Outcomes
You should be able to:
Schedule a recurring system job to count the number of active users.
Update the
systemdtimer unit that gathers system activity data.
Log in to workstation as student using student as the password.
On workstation, run lab scheduling-system start to start the exercise.
This script ensures that the environment is clean and set up correctly.
[student@workstation ~]$lab scheduling-system start
From
workstation, open an SSH session toserveraasstudent.[student@workstation ~]$ssh student@servera...output omitted...[student@servera ~]$Use the sudo -i command to switch to the
rootuser's account.[student@servera ~]$sudo -i[sudo] password for student:student[root@servera ~]#Schedule a recurring system job that generates a log message indicating the number of currently active users in the system. The job must run daily. You can use the w -h | wc -l command to retrieve the number of currently active users in the system. Also, use the logger command to generate the log message.
Create a script file called
/etc/cron.daily/usercountwith the following content. You can use the vi /etc/cron.daily/usercount command to create the script file.#!/bin/bash USERCOUNT=$(w -h | wc -l) logger "There are currently ${USERCOUNT} active users"Use the chmod command to enable the execute (
x) permission on/etc/cron.daily/usercount.[root@servera ~]#chmod +x /etc/cron.daily/usercount
The sysstat package provides the
systemdunits calledsysstat-collect.timerandsysstat-collect.service. The timer unit triggers the service unit every 10 minutes to collect system activity data using the shell script called/usr/lib64/sa/sa1. Make sure that the sysstat package is installed and change the timer unit configuration file to collect the system activity data every two minutes.Use the yum command to install the sysstat package.
[root@servera ~]#yum install sysstat...output omitted...Is this ok [y/N]:y...output omitted... Installed: sysstat-11.7.3-2.el8.x86_64 lm_sensors-libs-3.4.0-17.20180522git70f7e08.el8.x86_64 Complete!Copy
/usr/lib/systemd/system/sysstat-collect.timerto/etc/systemd/system/sysstat-collect.timer.[root@servera ~]#cp /usr/lib/systemd/system/sysstat-collect.timer \/etc/systemd/system/sysstat-collect.timerImportant
You should not edit files under the
/usr/lib/systemddirectory. Withsystemd, you can copy the unit file to the/etc/systemd/systemdirectory and edit that copy. Thesystemdprocess parses your customized copy instead of the file under the/usr/lib/systemddirectory.Edit
/etc/systemd/system/sysstat-collect.timerso that the timer unit runs every two minutes. Also, replace any occurrence of the string10 minuteswith2 minutesthroughout the unit configuration file including the ones in the commented lines. You may use the vi /etc/systemd/system/sysstat-collect.timer command to edit the configuration file....
# Activates activity collector every 2 minutes[Unit]Description=Run system activity accounting tool every 2 minutes[Timer]OnCalendar=*:00/02[Install] WantedBy=sysstat.serviceThe preceding changes cause the
sysstat-collect.timerunit to triggersysstat-collect.serviceunit every two minutes, which runs /usr/lib64/sa/sa1 1 1. Running /usr/lib64/sa/sa1 1 1 collects the system activity data in a binary file under the/var/log/sadirectory.Use the systemctl daemon-reload command to make sure that
systemdis aware of the changes.[root@servera ~]#systemctl daemon-reloadUse the systemctl command to activate the
sysstat-collect.timertimer unit.[root@servera ~]#systemctl enable --now sysstat-collect.timerUse the while command to wait until the binary file gets created under the
/var/log/sadirectory. Wait for your shell prompt to return.[root@servera ~]#while [ $(ls /var/log/sa | wc -l) -eq 0 ]; \do sleep 1s; doneIn the while command above, the ls /var/log/sa | wc -l returns a
0if the file does not exist and a1if it does exist. The while determines if this equals0and if so, enters the loop, which pauses for one second. When the file exists, the while loop exits.Use the ls -l command to verify that the binary file under the
/var/log/sadirectory got modified within last two minutes.[root@servera ~]#ls -l /var/log/satotal 8 -rw-r--r--. 1 root root 5156 Mar 2512:34sa25[root@servera ~]#dateMon Mar 25 12:35:32 +07 2019The output of the preceding commands may vary on your system.
Exit the
rootuser's shell and log out ofservera.[root@servera ~]#exitlogout[student@servera ~]$exitlogout Connection to servera closed.[student@workstation ~]$