Bookmark this page

Guided Exercise: Schedule Recurring User Jobs

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

  1. Log in to the servera machine as the student user .

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$
  2. 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.

    1. 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 ~]$ date
      Wed Mar 15 07:33:01 PM EDT 2023
      [student@servera ~]$

      Note

      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" +%a
      Tue
      [student@servera ~]$ date -d "next day" +%a
      Thu
    2. Open the crontab file with the default text editor.

      [student@servera ~]$ crontab -e
    3. Insert 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.txt
    4. 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 ~]$
  3. 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
  4. 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; done
  5. Verify 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 2023
  6. Remove all the scheduled recurring jobs for the student user.

    1. Remove all the scheduled recurring jobs for the student user.

      [student@servera ~]$ crontab -r
    2. Verify that no recurring jobs exist for the student user.

      [student@servera ~]$ crontab -l
      no crontab for student
    3. Return to the workstation machine as the student user.

      [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-cron

Revision: rh134-9.3-5fd2368