RHCSA Rapid Track
Course update
An updated version of this course is available that uses a newer version of Red Hat Enterprise Linux in the lab environment. Therefore, the RHEL 9.0 version of the lab environment will retire on December 31, 2024. Please complete any work in this lab environment before it is removed on December 31, 2024. For the most up-to-date version of this course, we recommend moving to the RHEL 9.3 version.
In this exercise, you schedule commands to run on a repeating schedule as a non-privileged user, with the crontab command.
Outcomes
Schedule recurring jobs to run as a non-privileged user.
Inspect the commands that a scheduled recurring job runs.
Remove scheduled recurring jobs.
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-cron
Instructions
Log in to the
serveramachine as thestudentuser .[student@workstation ~]$
ssh student@servera...output omitted... [student@servera ~]$Schedule a recurring job as the
studentuser that appends the current date and time to the/home/student/my_first_cron_job.txtfile every two minutes. Use thedatecommand to display the current date and time. The job must run only from one day before to one day after the current time. The job must not run on any other day.Use the
datecommand to display the current date and time. Note the day of the week, which you need for the next steps.[student@workstation ~]$
dateWedMar 15 07:33:01 PM EDT 2023 [student@servera ~]$Note
You can use the
date -d "last day" +%acommand to display the day before the current time, and thedate -d "next day" +%acommand to display the day after the current time.[student@servera ~]$
date -d "last day" +%aTue [student@servera ~]$date -d "next day" +%aThuOpen the
crontabfile with the default text editor.[student@servera ~]$
crontab -eInsert the following line. Replace the range of days from one day before to one day after the current time:
*/2 * * *
Tue-Thu/usr/bin/date >> /home/student/my_first_cron_job.txtPress Esc and type
:wqto save the changes and exit the editor. When the editor exits, you should see the following output:...output omitted... crontab: installing new crontab [student@servera ~]$
Use the
crontab -lcommand to list the scheduled recurring jobs. Inspect the command that you scheduled to run as a recurring job in the preceding step.Verify that the job runs the
/usr/bin/datecommand and appends its output to the/home/student/my_first_cron_job.txtfile.[student@servera ~]$
crontab -l*/2 * * *Tue-Thu/usr/bin/date >> /home/student/my_first_cron_job.txtInstruct your shell prompt to sleep until the
/home/student/my_first_cron_job.txtfile is created because of the successful execution of the recurring job that you scheduled. Wait for your shell prompt to return.The
whilecommand uses! test -fto continue to run a loop, and sleeps for one second until themy_first_cron_job.txtfile is created in the/home/studentdirectory.[student@servera ~]$
while ! test -f my_first_cron_job.txt; do sleep 1s; doneVerify that the contents of the
/home/student/my_first_cron_job.txtfile match the output of thedatecommand.[student@servera ~]$
cat my_first_cron_job.txtWed Mar 15 07:40:01 PM EDT 2023Remove all the scheduled recurring jobs for the
studentuser.Remove all the scheduled recurring jobs for the
studentuser.[student@servera ~]$
crontab -rVerify that no recurring jobs exist for the
studentuser.[student@servera ~]$
crontab -lno crontab for studentReturn to the
workstationmachine as thestudentuser.[student@servera ~]$
exitlogout Connection to servera closed. [student@workstation ~]$
This concludes the section.