Bookmark this page

Guided Exercise: Scheduling a Deferred User Job

In this exercise, you will use the at command to schedule several commands to run at specified times in the future.

Outcomes

You should be able to:

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

  • Inspect the commands that a scheduled job runs.

  • Delete the scheduled jobs.

Log in to workstation as student using student as the password.

On workstation, run lab scheduling-at start to start the exercise. This script ensures that the environment is clean and set up correctly.

[student@workstation ~]$ lab scheduling-at start
  1. From workstation, open an SSH session to servera as student.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$ 
  2. Schedule a job to run in three minutes from now using the at command. The job must save the output of the date command to /home/student/myjob.txt.

    1. Use the echo command to pass the string date >> /home/student/myjob.txt as input to the at command so that the job runs in three minutes from now.

      [student@servera ~]$ echo "date >> /home/student/myjob.txt" | at now +3min
      warning: commands will be executed using /bin/sh
      job 1 at Thu Mar 21 12:30:00 2019
    2. Use the atq command to list the scheduled jobs.

      [student@servera ~]$ atq
      1 Thu Mar 21 12:30:00 2019 a student
    3. Use the watch atq command to command to monitor the queue of the deferred jobs in real time. The job is removed from the queue after its execution.

      [student@servera ~]$ watch atq
      Every 2.0s: atq         servera.lab.example.com: Thu Mar 21 12:30:00 2019
      
      1 Thu Mar 21 12:30:00 2019 a student

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

    4. Use the cat command to verify that the contents of /home/student/myjob.txt match the output of the date command.

      [student@servera ~]$ cat myjob.txt
      Thu Mar 21 12:30:00 IST 2019

      The preceding output matches with the output of the date command, confirming that the scheduled job executed successfully.

  3. Use the at command to interactively schedule a job with the queue g that runs at teatime (16:00). The job should execute a command that prints the message It's teatime to /home/student/tea.txt. The new messages should be appended to the file /home/student/tea.txt.

    [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 Thu Mar 21 16:00:00 2019
  4. Use the at command to interactively schedule another job with the queue b that runs at 16:05. The job should execute a command that prints the message The cookies are good to /home/student/cookies.txt. The new messages should be appended to the file /home/student/cookies.txt.

    [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 Thu Mar 21 16:05:00 2019
  5. Inspect the commands in the pending jobs.

    1. Use the atq command to view the job numbers of the pending jobs.

      [student@servera ~]$ atq
      2 Thu Mar 21 16:00:00 2019 g student
      3 Thu Mar 21 16:05:00 2019 b student

      Note the job numbers in the preceding output. These job numbers may vary on your system.

    2. Use the at command to view the commands in the pending job number 2.

      [student@servera ~]$ at -c 2
      ...output omitted...
      echo "It's teatime" >> /home/student/tea.txt
      marcinDELIMITER28d54caa

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

    3. Use the at command to view the commands in the pending job number 3.

      [student@servera ~]$ at -c 3
      ...output omitted...
      echo "The cookies are good" >> /home/student/cookies.txt
      marcinDELIMITER1d2b47e9

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

  6. Use the atq command to view the job number of a job that runs at teatime (16:00) and remove it using the atrm command.

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

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

      [student@servera ~]$ atq
      3 Thu Mar 21 16:05:00 2019 b student
    2. Log off from servera.

      [student@servera ~]$ exit
      logout
      Connection to servera closed.
      [student@workstation ~]$ 

Finish

On workstation, run lab scheduling-at finish to complete this exercise. This script deletes the files created throughout the exercise and ensures that the environment is clean.

[student@workstation ~]$ lab scheduling-at finish

This concludes the guided exercise.

Revision: rh134-8.2-f0a9756