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 servera machine as the student user .
[student@workstation ~]$ ssh student@servera
...output omitted...
[student@servera ~]$Schedule a recurring job as the student user that appends the current date and time to the /home/student/my_first_cron_job.txt file every two minutes.
Use the date command 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 date command 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 ~]$
You can use the date -d "last day" +%a command to display the day before the current time, and the date -d "next day" +%a command to display the day after the current time.
[student@servera ~]$date -d "last day" +%aTue [student@servera ~]$date -d "next day" +%aThu
Open the crontab file 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 :wq to 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 -l command 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/date command and appends its output to the /home/student/my_first_cron_job.txt file.
[student@servera ~]$crontab -l*/2 * * *Tue-Thu/usr/bin/date >> /home/student/my_first_cron_job.txt
Instruct your shell prompt to sleep until the /home/student/my_first_cron_job.txt file is created because of the successful execution of the recurring job that you scheduled.
Wait for your shell prompt to return.
The while command uses ! test -f to continue to run a loop, and sleeps for one second until the my_first_cron_job.txt file is created in the /home/student directory.
[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.txt file match the output of the date command.
[student@servera ~]$ cat my_first_cron_job.txt
Wed Mar 15 07:40:01 PM EDT 2023Remove all the scheduled recurring jobs for the student user.
Remove all the scheduled recurring jobs for the student user.
[student@servera ~]$ crontab -rVerify that no recurring jobs exist for the student user.
[student@servera ~]$ crontab -l
no crontab for studentReturn to the workstation machine as the student user.
[student@servera ~]$ exit
logout
Connection to servera closed.
[student@workstation ~]$This concludes the section.