In this exercise, you will schedule commands to run on a repeating schedule as a non-privileged user, using the crontab command.
Outcomes
You should be able to:
Schedule recurring jobs to run as a non-privileged user.
Inspect the commands that a scheduled recurring job runs.
Remove scheduled recurring jobs.
Log in to workstation as student using student as the password.
On workstation, run lab scheduling-cron start to start the exercise.
This script ensures that the environment is clean and set up correctly.
[student@workstation ~]$lab scheduling-cron start
From workstation, open an SSH session to servera as student.
[student@workstation ~]$ssh student@servera...output omitted...[student@servera ~]$
Schedule a recurring job as student that appends the current date and time to /home/student/my_first_cron_job.txt every two minutes between 8 a.m. and 9 p.m.
The job must only run from Monday to Friday, not on Saturday or Sunday.
If you are working on this lab outside of the day and time mentioned in the preceding instruction, you should adjust your system time and/or date accordingly so that the job runs while you are working.
Use the crontab -e command to open the crontab using the default text editor.
[student@servera ~]$crontab -e
Insert the following line.
*/2 08-20 * * Mon-Fri /usr/bin/date >> /home/student/my_first_cron_job.txt
While in the text editor, press 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 ~]$
The preceding output confirms that the job was scheduled successfully.
Use the crontab -l command to list the scheduled recurring jobs. Inspect the command you scheduled to run as a recurring job in the preceding step.
[student@servera ~]$crontab -l*/2 08-20 * * Mon-Fri/usr/bin/date >> /home/student/my_first_cron_job.txt
Notice that the preceding scheduled job runs the /usr/bin/date command and appends its output to /home/student/my_first_cron_job.txt.
Use the while command so that your shell prompt sleeps until the /home/student/my_first_cron_job.txt file is created as a result of the successful execution of the recurring job you scheduled.
Wait for your shell prompt to return.
[student@servera ~]$while ! test -f my_first_cron_job.txt; do sleep 1s; done
The preceding while command uses ! test -f to continue running a loop of sleep 1s commands until the my_first_cron_job.txt file is created in the /home/student directory.
Use the cat command to verify that the contents of /home/student/my_first_cron_job.txt match the output of the date command.
[student@servera ~]$cat my_first_cron_job.txtFri Mar 22 13:56:01 IST 2019
The preceding output may vary on your system.
Remove all the recurring jobs scheduled to run as student.
Use the crontab -r command to remove all the scheduled recurring jobs for student.
[student@servera ~]$crontab -r
Use the crontab -l command to verify that no recurring jobs exist for student.
[student@servera ~]$crontab -lno crontab for student
Log off from servera.
[student@servera ~]$exitlogout Connection to servera closed.[student@workstation ~]$