Linux All-in-One For Dummies
Book image
Explore Book Buy On Amazon
As a Linux system administrator, you may have to run some programs automatically at regular intervals or execute one or more commands at a specified time in the future. Your Linux system includes the facilities to schedule jobs to run at any future date or time you want. You can also set up the system to perform a task periodically or just once. Here are some typical tasks you can perform by scheduling jobs on your Linux system:
  • Back up the files in the middle of the night.
  • Download large files in the early morning when the system isn’t busy.
  • Send yourself messages as reminders of meetings.
  • Analyze system logs periodically and look for any abnormal activities.
You can set up these jobs by using the at command or the crontab facility of Linux.

How to schedule one-time jobs in Linux

If you want to run one or more commands at a later time, you can use the at command. The atd daemon — a program designed to process jobs submitted with at — runs your commands at the specified time and mails the output to you.

Before you try the at command in Linux, you need to know that the following configuration files control which users can schedule tasks by using the at command:

  • /etc/at.allow contains the names of the users who may use the at command to submit jobs.
  • /etc/at.deny contains the names of users who are not allowed to use the at command to submit jobs.
If these files aren’t present, or if you find an empty /etc/at.deny file, any user can submit jobs by using the at command. The default in Linux is an empty /etc/at.deny file; when this default is in place, anyone can use the at command. If you don’t want some users to use at, simply list their usernames in the /etc/at.deny file.

To use at to schedule a one-time job in Linux for execution at a later time, follow these steps:

  1. Run the at command with the date or time when you want your commands to be executed.

    When you press Enter, the at> prompt appears, as follows:

    at 21:30
    at>
    This method is the simplest way to indicate the time when you want to execute one or more commands; simply specify the time in a 24-hour format. In this case, you want to execute the commands at 9:30 tonight (or tomorrow, if it’s already past 9:30 p.m.). You can, however, specify the execution time in many ways.
  2. At the at> prompt, type the commands you want to execute as though you were typing at the shell prompt. After each command, press Enter and continue with the next command.
  3. When you finish entering the commands you want to execute, press Ctrl+D to indicate the end.

    Here’s an example that shows how to execute the ps command at a future time:

    at> ps
    at> <EOT>
    job 1 at 2018-12-28 21:30
    After you press Ctrl+D, the at command responds with the <EOT> message, a job number, and the date and time when the job will execute.
Formats for the at Command for the Time of Execution
Command When the Job Will Run
at now Immediately
at now + 15 minutes 15 minutes from the current time
at now + 4 hours 4 hours from the current time
at now + 7 days 7 days from the current time
at noon At noon today (or tomorrow, if it’s already past noon)
at now next hour Exactly 60 minutes from now
at now next day At the same time tomorrow
at 17:00 tomorrow At 5:00 p.m. tomorrow
at 4:45pm At 4:45 p.m. today (or tomorrow, if it’s already past 4:45 p.m.)
at 3:00 Dec 28, 2018 At 3:00 a.m. on December 28, 2018
After you enter one or more jobs, you can view the current list of scheduled jobs with the atq command. The output of this command looks similar to the following:
4 2018-12-28 03:00 a root
5 2018-10-26 21:57 a root
6 2018-10-26 16:45 a root
The first field in each line shows the job number — the same number that the at command displays when you submit the job. The next field shows the year, month, day, and time of execution. The last field shows the jobs pending in the a queue and the username.

If you want to cancel a job, use the atrm command to remove that job from the queue. When you’re removing a job with the atrm command, refer to the job by its number, as follows:

atrm 4
This command deletes job 4 scheduled for 3:00 a.m. on December 28, 2018.]

When a job executes, the output is mailed to you. Type mail at a terminal window to read your mail and to view the output from your jobs.

How to schedule recurring jobs in Linux

Although at is good for running commands at a specific time, it’s not useful for running a program automatically at repeated intervals. You have to use crontab to schedule such recurring jobs, such as if you want to back up your files to tape at midnight every evening.

You schedule recurring jobs by placing job information in a file with a specific format and submitting this file with the crontab command. The cron daemon — crond — checks the job information every minute and executes the recurring jobs at the specified times. Because the cron daemon processes recurring jobs, such jobs are also referred to as cron jobs.

Any output from a cron job is mailed to the user who submits the job. (In the submitted job-information file, you can specify a different recipient for the mailed output.)

Two configuration files control who can schedule cron jobs in Linux by using crontab:

  • /etc/cron.allow contains the names of the users who are allowed to use the crontab command to submit jobs.
  • /etc/cron.deny contains the names of users who are not allowed to use the crontab command to submit jobs.
If the /etc/cron.allow file exists, only users listed in this file can schedule cron jobs. If only the /etc/cron.deny file exists, users listed in this file can’t schedule cron jobs. If neither file exists, the default Linux setup enables any user to submit cron jobs.

To submit a cron job in Linux, follow these steps:

  1. Prepare a shell script (or an executable program in any programming language) that can perform the recurring task you want to perform.

    You can skip this step if you want to execute an existing program periodically.

  2. Prepare a text file with information about the times when you want the shell script or program (from Step 1) to execute; then submit this file by using crontab.

    You can submit several recurring jobs with a single file. Each line with timing information about a job has a standard format, with six fields. The first five fields specify when the job runs, and the sixth and subsequent fields constitute the command that runs. Here’s a line that executes the myjob shell script in a user’s home directory at 5 minutes past midnight each day:

    5 0 * * * $HOME/myjob
    The table below shows the meaning of the first five fields. Note: An asterisk (*) means all possible values for that field. Also, an entry in any of the first five fields can be a single number, a comma-separated list of numbers, a pair of numbers separated by a hyphen (indicating a range of numbers), or an asterisk.
Format for the Time of Execution in crontab Files
Field Number Meaning of Field Acceptable Range of Values*
1 Minute 0–59
2 Hour of the day 0–23
3 Day of the month 0–31
4 Month 1–12 (1 means January, 2 means February, and so on) or the names of months using the first three letters — Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
5 Day of the week 0–6 (0 means Sunday, 1 means Monday, and so on) or the three-letter abbreviations of weekdays — Sun, Mon, Tue, Wed, Thu, Fri, Sat
* An asterisk in a field means all possible values for that field. If an asterisk is in the third field, for example, the job is executed every day.

If the text file jobinfo (in the current directory) contains the job information, submit this information to crontab with the following command:

crontab jobinfo
That’s it! You’re set with the cron job. From now on, the cron job runs at regular intervals (as specified in the job-information file), and you receive mail messages with the output from the job.

To verify that the job is indeed scheduled in Linux, type the following command:

crontab -l
The output of the crontab -l command shows the cron jobs currently installed in your name. To remove your cron jobs, type crontab -r.

If you log in as root, you can also set up, examine, and remove cron jobs for any user. To set up cron jobs for a user, use this command:

crontab _u <em>username filename</em>
Here, username is the user for whom you install the cron jobs, and filename is the file that contains information about the jobs.

Use the following form of the crontab command to view the cron jobs for a user:

crontab _u <em>username</em> -l
To remove a user’s cron jobs, use the following command:
crontab -u <em>username</em> -r
Note: The cron daemon also executes the cron jobs listed in the systemwide cron job file /etc/crontab. Here’s a typical /etc/crontab file from a Linux system (type cat /etc/crontab to view the file):
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
The first four lines set up several environment variables for the jobs listed in this file. The MAILTO environment variable specifies the user who receives the mail message with the output from the cron jobs in this file.

The line that begins with # is a comment line. The four lines following the run-parts comment execute the run-parts shell script (located in the /usr/bin directory) at various times with the name of a specific directory as argument. Each argument to run-parts/etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly — is a directory. Essentially, run-parts executes all scripts located in the directory that you provide as an argument.

The table below lists the directories where you can find these scripts and when they execute. You have to look at the scripts in these directories to know what executes at these intervals.

Script Directories for cron Jobs

Directory Name Script Executes
/etc/cron.hourly Every hour
/etc/cron.daily Each day
/etc/cron.weekly Weekly
/etc/cron.monthly Once each month

About This Article

This article is from the book:

About the book author:

Emmett Dulaney is a university professor and columnist for Certification Magazine. An expert on operating systems and certification, he is the author of CompTIA Security+ Study Guide, CompTIA A+ Complete Study Guide, and CompTIA Network+ Exam Cram.

This article can be found in the category: