Skip to content

Practice Exercise: Starting Processes in the Future

Objectives

  • Learn how to schedule and start processes at a later time using at and cron.
  • Gain practical experience in automating tasks on a Linux system.

Scenario

Automation is a key aspect of Linux system administration. In this practice exercise, you will explore two tools, at and cron, which allow you to schedule and start processes at specific times or on a recurring basis. These tools are valuable for automating routine tasks and managing system processes.

Before you begin...

Make sure that you have read the Lab Environment before starting the exercise.

Tasks

Task 1: Using at to Schedule a One-Time Task

  • Open a terminal on your Linux system.
  • Execute the at command followed by a specific time when you want a task to run (e.g., at 14:30 to run the task at 2:30 PM).
  • Say from now + 1 minute
  • What is the command for the above condition?
  • In the at> prompt, enter a command you want to execute at the specified time (e.g., echo "Hello, world" > ~/hello.txt).
  • Press Ctrl+D to schedule the task.
  • Verify that the task is scheduled using the atq command.
  • Wait for the specified time, and observe the task execution.
[intern@intern-a1t-inf-lnx1 ~]$ at now + 2 minutes
warning: commands will be executed using /bin/sh
at Mon Sep 18 00:47:00 2023
at> echo "Hello world! $(date)" > ~/hello.txt
# Press ctrl + d
at> <EOT>
job 2 at Mon Sep 18 00:47:00 2023
[intern@intern-a1t-inf-lnx1 ~]$ atq
2   Mon Sep 18 00:47:00 2023 a intern
[intern@intern-a1t-inf-lnx1 ~]$ cat ~/hello.txt
Hello world! Mon Sep 18 00:44:20 UTC 202

Task 2: Using cron to Schedule Recurring Tasks

  • Use the crontab -e command to open your user's crontab file for editing.
  • Add a new line to schedule a recurring task (e.g., 0 3 * * * /usr/bin/backup-script.sh to run a backup script every day at 3:00 AM).
  • Save and exit the crontab file.
  • Verify that the new cron job is added by running crontab -l.
  • Wait for the scheduled time, and observe the recurring task execution.
  • Optionally, you can edit the crontab file to schedule different tasks at specific intervals.

[intern@intern-a1t-inf-lnx1 ~]$ crontab -e
no crontab for intern - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2
- If you haven't change your default editor yet you will receive this from the output. Select the editor you are most comfortable with. - After selecting an editor you can now edit the crontab file
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
0 3 * * * /usr/bin/backup-script.sh
~
"/tmp/crontab.CgJXDF/crontab" 23L, 889B
- Save the update and exit
[intern@intern-a1t-inf-lnx1 ~]$ crontab -e
no crontab for intern - using an empty one
crontab: installing new crontab
[intern@intern-a1t-inf-lnx1 ~]$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
0 3 * * * /usr/bin/backup-script.sh

Task 3: Managing Cron Jobs

  • Use the crontab -e command to open your user's crontab file for editing.
  • Remove or comment out one of the existing cron jobs.
  • Save and exit the crontab file.
  • Verify that the removed or commented-out cron job is no longer active by running crontab -l.
  • Check the crontab file inside /etc
[intern@intern-a1t-inf-lnx1 ~]$ crontab -e
crontab: installing new crontab
.....
# 0 3 * * * /usr/bin/backup-script.sh
[intern@intern-a1t-inf-lnx1 ~]$ crontab -l
.....
# 0 3 * * * /usr/bin/backup-script.sh
[intern@intern-a1t-inf-lnx1 ~]$ sudo ls /etc | grep cron
cron.d
cron.daily
cron.hourly
cron.monthly
cron.weekly
crontab

Task 4: Exploring at and cron Configuration Files

  • Explore the /etc/crontab file to see system-wide cron jobs.
  • View the contents of the /etc/at.allow and /etc/cron.allow files, if they exist, to understand user access control for at and cron.
  • Similarly, explore the /etc/at.deny and /etc/cron.deny files, if they exist, to see denied access rules.
  • Understand the format of cron job entries in the /etc/crontab file.

[intern@intern-a1t-inf-lnx1 ~]$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
# You can also override PATH, but by default, newer versions inherit it from the environment
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

Conclusion

This practice exercise has provided you with practical experience in scheduling and starting processes in the future using the at and cron tools on a Linux system. You've learned how to schedule one-time tasks with at and recurring tasks with cron. Additionally, you've explored the configuration files and access control mechanisms associated with these tools. Automation through at and cron is essential for efficiently managing system tasks and maintenance.