Bookmark this page

Guided Exercise: Schedule a Deferred User Job

Use the at command to schedule several commands to run at specified future times.

Outcomes

  • Schedule a job to run at a specified future time.

  • Inspect the commands that a scheduled job runs.

  • Delete the scheduled 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-at

Instructions

  1. From workstation, open an SSH session to servera as the student user.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$
  2. Schedule a job to run in two minutes from now. Save the output of the date command to the /home/student/myjob.txt file.

    1. Pass the date >> /home/student/myjob.txt string as the input to the at command, so that the job runs in two minutes from now.

      [student@servera ~]$ echo "date >> /home/student/myjob.txt" | at now +2min
      warning: commands will be executed using /bin/sh
      job 1 at Thu Feb 16 18:51:16 2023
    2. List the scheduled jobs.

      [student@servera ~]$ atq
      1       Thu Feb 16 18:51:16 2023 a student
    3. Monitor the deferred jobs queue in real time. After the atd daemon executes, it removes the job from the queue.

      The command updates the output of the atq command every two seconds, by default. After the atd daemon removes the deferred job from the queue, press Ctrl+c to exit the watch command and return to the shell prompt.

      [student@servera ~]$ watch atq
      Every 2.0s: atq              servera.lab.example.com: Thu Feb 16 17:58:50 2023
      
      1       Thu Feb 16 18:51:16 2023 a student
    4. Verify that the contents of the /home/student/myjob.txt file match the output of the date command.

      The output matches the output of the date command, which confirms that the scheduled job executed successfully.

      [student@servera ~]$ cat myjob.txt
      Thu Feb 16 06:51:16 PM EDT 2023
  3. Interactively schedule a job in the g queue that runs at teatime (16:00). The job should print the It's teatime message to the /home/student/tea.txt file. Append the new messages to the /home/student/tea.txt file.

    [student@servera ~]$ at -q g teatime
    warning: commands will be executed using /bin/sh
    at> echo "It's teatime" >> /home/student/tea.txt
    at> Ctrl+d
    job 2 at Fri Feb 17 16:00:00 2023
  4. Interactively schedule another job with the b queue that runs at 16:05. The job should print The cookies are good message to the /home/student/cookies.txt file. Append the new messages to the /home/student/cookies.txt file.

    [student@servera ~]$ at -q b 16:05
    warning: commands will be executed using /bin/sh
    at> echo "The cookies are good" >> /home/student/cookies.txt
    at> Ctrl+d
    job 3 at Fri Feb 17 16:05:00 2023
  5. Inspect the commands in the pending jobs.

    1. View the job numbers of the pending jobs.

      Note the job numbers in the output, which might vary on your system. Use the job numbers from your system.

      [student@servera ~]$ atq
      2       Fri Feb 17 16:00:00 2023 g student
      3       Fri Feb 17 16:05:00 2023 b student
    2. View the commands in the pending job number 2. Replace the job number if it changed for you.

      The job executes an echo command that appends the It's teatime message to the /home/student/tea.txt file.

      [student@servera ~]$ at -c 2
      ...output omitted...
      echo "It's teatime" >> /home/student/tea.txt
      marcinDELIMITER1d7be6a7
    3. View the commands in the pending job number 3. Replace the job number if it changed for you.

      The job executes an echo command that appends the message The cookies are good to the /home/student/cookies.txt file.

      [student@servera ~]$ at -c 3
      ...output omitted...
      echo "The cookies are good" >> /home/student/cookies.txt
      marcinDELIMITER44662c6f
  6. View the job number of a job that runs at teatime (16:00), and remove it by using the atrm command.

    [student@servera ~]$ atq
    2       Fri Feb 17 16:00:00 2023 g student
    3       Fri Feb 17 16:05:00 2023 b student
    [student@servera ~]$ atrm 2
  7. Verify that the scheduled job to run at teatime (16:00) no longer exists.

    1. View the list of pending jobs, and confirm that the scheduled job to run at teatime (16:00) no longer exists.

      [student@servera ~]$ atq
      3       Fri Feb 17 16:05:00 2023 b student
    2. 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-at

Revision: rh134-9.3-5fd2368