Este contenido no está disponible en el idioma seleccionado.
Chapter 24. Automating System Tasks
You can configure Red Hat Enterprise Linux to automatically run tasks, also known as jobs:
- regularly at specified time using cron, see Section 24.1, “Scheduling a Recurring Job Using Cron”
- asynchronously at certain days using anacron, see Section 24.2, “Scheduling a Recurring Asynchronous Job Using Anacron”
- once at a specific time using at, see Section 24.3, “Scheduling a Job to Run at a Specific Time Using at”
- once when system load average drops to a specified value using batch, see Section 24.4, “Scheduling a Job to Run on System Load Drop Using batch”
- once on the next boot, see Section 24.5, “Scheduling a Job to Run on Next Boot Using a systemd Unit File”
This chapter describes how to perform these tasks.
24.1. Scheduling a Recurring Job Using Cron Copiar enlaceEnlace copiado en el portapapeles!
Cron is a service that enables you to schedule running a task, often called a job, at regular times. A cron job is only executed if the system is running on the scheduled time. For scheduling jobs that can postpone their execution to when the system boots up, so a job is not "lost" if the system is not running, see Section 24.3, “Scheduling a Job to Run at a Specific Time Using at”.
Users specify cron jobs in cron table files, also called crontab files. These files are then read by the crond service, which executes the jobs.
24.1.1. Prerequisites for Cron Jobs Copiar enlaceEnlace copiado en el portapapeles!
Before scheduling a cron job:
Install the cronie package:
yum install cronie
~]# yum install cronieCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
crondservice is enabled - made to start automatically at boot time - upon installation. If you disabled the service, enable it:systemctl enable crond.service
~]# systemctl enable crond.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
crondservice for the current session:systemctl start crond.service
~]# systemctl start crond.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow (optional) Configure cron. For example, you can change:
- shell to be used when executing jobs
-
the
PATHenvironment variable mail addressee if a job sends emails.
See the crontab(5) manual page for information on configuring
cron.
24.1.2. Scheduling a Cron Job Copiar enlaceEnlace copiado en el portapapeles!
Scheduling a Job as root User
The root user uses the cron table in /etc/crontab, or, preferably, creates a cron table file in /etc/cron.d/. Use this procedure to schedule a job as root:
Choose:
-
in which minutes of an hour to execute the job. For example, use
0,10,20,30,40,50or0/10to specify every 10 minutes of an hour. -
in which hours of a day to execute the job. For example, use
17-20to specify time from 17:00 to 20:59. -
in which days of a month to execute the job. For example, use
15to specify 15th day of a month. -
in which months of a year to execute the job. For example, use
Jun,Jul,Augor6,7,8to specify the summer months of the year. in which days of the week to execute the job. For example, use
*for the job to execute independently of the day of week.Combine the chosen values into the time specification. The above example values result into this specification:
0,10,20,30,40,50 17-20 15 Jun,Jul,Aug *
-
in which minutes of an hour to execute the job. For example, use
-
Specify the user. The job will execute as if run by this user. For example, use
root. -
Specify the command to execute. For example, use
/usr/local/bin/my-script.sh Put the above specifications into a single line:
0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * root /usr/local/bin/my-script.sh
0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * root /usr/local/bin/my-script.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Add the resulting line to
/etc/crontab, or, preferably, create a cron table file in/etc/cron.d/and add the line there.
The job will now run as scheduled.
For full reference on how to specify a job, see the crontab(5) manual page. For basic information, see the beginning of the /etc/crontab file:
Scheduling a Job as Non-root User
Non-root users can use the crontab utility to configure cron jobs. The jobs will run as if executed by that user.
To create a cron job as a specific user:
From the user’s shell, run:
crontab -e
[bob@localhost ~]$ crontab -eCopy to Clipboard Copied! Toggle word wrap Toggle overflow This will start editing of the user’s own
crontabfile using the editor specified by theVISUALorEDITORenvironment variable.Specify the job in the same way as in the section called “Scheduling a Job as root User”, but leave out the field with user name. For example, instead of adding
0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * bob /home/bob/bin/script.sh
0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * bob /home/bob/bin/script.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow add:
0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * /home/bob/bin/script.sh
0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * /home/bob/bin/script.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Save the file and exit the editor.
(optional) To verify the new job, list the contents of the current user’s crontab file by running:
crontab -l
[bob@localhost ~]$ crontab -l @daily /home/bob/bin/script.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Scheduling Hourly, Daily, Weekly, and Monthly Jobs
To schedule an hourly, daily, weekly, or monthly job:
- Put the actions you want your job to execute into a shell script.
Put the shell script into one of the following directories:
-
/etc/cron.hourly/ -
/etc/cron.daily/ -
/etc/cron.weekly/ -
/etc/cron.monthly/
-
From now, your script will be executed - the crond service automatically executes any scripts present in /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly directories at their corresponding times.
24.2. Scheduling a Recurring Asynchronous Job Using Anacron Copiar enlaceEnlace copiado en el portapapeles!
Anacron, like cron, is a service that enables you to schedule running a task, often called a job, at regular times. However, anacron differs from cron in two ways:
-
If the system is not running at the scheduled time, an
anacronjob is postponed until the system is running; -
An
anacronjob can run once per day at most.
Users specify anacron jobs in anacron table files, also called anacrontab files. These files are then read by the crond service, which executes the jobs.
24.2.1. Prerequisites for Anacrob Jobs Copiar enlaceEnlace copiado en el portapapeles!
Before scheduling an anacron job:
Verify that you have the cronie-anacron package installed:
rpm -q cronie-anacron
~]# rpm -q cronie-anacronCopy to Clipboard Copied! Toggle word wrap Toggle overflow The cronie-anacron is likely to be installed already, because it is a sub-package of the cronie package. If it is not installed, use this command:
yum install cronie-anacron
~]# yum install cronie-anacronCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
crondservice is enabled - made to start automatically at boot time - upon installation. If you disabled the service, enable it:systemctl enable crond.service
~]# systemctl enable crond.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
crondservice for the current session:systemctl start crond.service
~]# systemctl start crond.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow (optional) Configure anacron. For example, you can change:
- shell to be used when executing jobs
-
the
PATHenvironment variable mail addressee if a job sends emails.
See the anacrontab(5) manual page for information on configuring
anacron.
By default, the anacron configuration includes a condition that prevents it from running if the computer is not plugged in. This setting ensures that the battery is not drained by running anacron jobs.
If you want to allow anacron to run even if the computer runs on battery power, open the /etc/cron.hourly/0anacron file and comment out the following part:
24.2.2. Scheduling an Anacron Job Copiar enlaceEnlace copiado en el portapapeles!
Scheduling an anacron Job as root User
The root user uses the anacron table in /etc/anacrontab. Use the following procedure to schedule a job as root.
Scheduling an anacron Job as root User
Choose:
-
Frequency of executing the job. For example, use
1to specify every day or3to specify once in 3 days. -
The delay of executing the job. For example, use
0to specify no delay or60to specify 1 hour of delay. -
The job identifier, which will be used for logging. For example, use
my.anacron.jobto log the job with themy.anacron.jobstring. The command to execute. For example, use
/usr/local/bin/my-script.shCombine the chosen values into the job specification. Here is an example specification:
3 60 cron.daily /usr/local/bin/my-script.sh
3 60 cron.daily /usr/local/bin/my-script.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Frequency of executing the job. For example, use
-
Add the resulting line to
/etc/anacrontab.
The job will now run as scheduled.
For simple job examples, see the /etc/anacrontab file. For full reference on how to specify a job, see the anacrontab(5) manual page.
Scheduling Hourly, Daily, Weekly, and Monthly Jobs
You can schedule daily, weekly, and monthly jobs with anacron. See the section called “Scheduling Hourly, Daily, Weekly, and Monthly Jobs”.
24.3. Scheduling a Job to Run at a Specific Time Using at Copiar enlaceEnlace copiado en el portapapeles!
To schedule a one-time task, also called a job, to run once at a specific time, use the at utility.
Users specify at jobs using the at utility. The jobs are then executed by the atd service.
24.3.1. Prerequisites for At Jobs Copiar enlaceEnlace copiado en el portapapeles!
Before scheduling an at job:
Install the at package:
yum install at
~]# yum install atCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
atdservice is enabled - made to start automatically at boot time - upon installation. If you disabled the service, enable it:systemctl enable atd.service
~]# systemctl enable atd.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
atdservice for the current session:systemctl start atd.service
~]# systemctl start atd.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
24.3.2. Scheduling an At Job Copiar enlaceEnlace copiado en el portapapeles!
A job is always run by some user. Log in as the desired user and run:
at time
~]# at timeCopy to Clipboard Copied! Toggle word wrap Toggle overflow Replace time with the time specification.
For details on specifying time, see the at(1) manual page and the
/usr/share/doc/at/timespecfile.Example 24.1. Specifying Time for At
To execute the job at 15:00, run:
at 15:00
~]# at 15:00Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the specified time has passed, the job is executed at the same time the next day.
To execute the job on August 20 2017, run:
at August 20 2017
~]# at August 20 2017Copy to Clipboard Copied! Toggle word wrap Toggle overflow or
at 082017
~]# at 082017Copy to Clipboard Copied! Toggle word wrap Toggle overflow To execute the job 5 days from now, run:
now + 5 days
~]# now + 5 daysCopy to Clipboard Copied! Toggle word wrap Toggle overflow At the displayed
at>prompt, enter the command to execute and press Enter:at 15:00
~]# at 15:00 at> sh /usr/local/bin/my-script.sh at>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Repeat this step for every command you want to execute.
NoteThe
at>prompt shows which shell it will use:warning: commands will be executed using /bin/sh
warning: commands will be executed using /bin/shCopy to Clipboard Copied! Toggle word wrap Toggle overflow The at utility uses the shell set in user’s SHELL environment variable, or the user’s login shell, or
/bin/sh, whichever is found first.- Press Ctrl+D on an empty line to finish specifying the job.
If the set of commands or the script tries to display information to standard output, the output is emailed to the user.
Viewing Pending Jobs
To view the list of pending jobs, use the atq command:
atq
~]# atq
26 Thu Feb 23 15:00:00 2017 a root
28 Thu Feb 24 17:30:00 2017 a root
Each job is listed on a separate line in the following format:
job_number scheduled_date scheduled_hour job_class user_name
job_number scheduled_date scheduled_hour job_class user_name
The job_queue column specifies whether a job is an at or a batch job. a stands for at, b stands for batch.
Non-root users only see their own jobs. The root user sees jobs for all users.
Deleting a Scheduled Job
To delete a scheduled job:
List pending jobs with the
atqcommand:atq
~]# atq 26 Thu Feb 23 15:00:00 2017 a root 28 Thu Feb 24 17:30:00 2017 a rootCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Find the job you want to delete by its scheduled time and the user.
Run the
atrmcommand, specifying the job by its number:atrm 26
~]# atrm 26Copy to Clipboard Copied! Toggle word wrap Toggle overflow
24.3.2.1. Controlling Access to At and Batch Copiar enlaceEnlace copiado en el portapapeles!
You can restrict access to the at and batch commands for specific users. To do this, put user names into /etc/at.allow or /etc/at.deny according to these rules:
- Both access control files use the same format: one user name on each line.
- No white space is permitted in either file.
-
If the
at.allowfile exists, only users listed in the file are allowed to useatorbatch, and theat.denyfile is ignored. -
If
at.allowdoes not exist, users listed inat.denyare not allowed to useatorbatch. -
The
rootuser is not affected by the access control files and can always execute theatandbatchcommands.
The at daemon (atd) does not have to be restarted if the access control files are modified. The access control files are read each time a user tries to execute the at or batch commands.
24.4. Scheduling a Job to Run on System Load Drop Using batch Copiar enlaceEnlace copiado en el portapapeles!
To schedule a one-time task, also called a job, to run when the system load average drops below the specified value, use the batch utility. This can be useful for performing resource-demanding tasks or for preventing the system from being idle.
Users specify batch jobs using the batch utility. The jobs are then executed by the atd service.
24.4.1. Prerequisites for Batch Jobs Copiar enlaceEnlace copiado en el portapapeles!
The batch utility is provided in the at package, and batch jobs are managed by the atd service. Hence, the prerequisites for batch jobs are the same as for at jobs. See Section 24.3.1, “Prerequisites for At Jobs”.
24.4.2. Scheduling a Batch Job Copiar enlaceEnlace copiado en el portapapeles!
A job is always run by some user. Log in as the desired user and run:
batch
~]# batchCopy to Clipboard Copied! Toggle word wrap Toggle overflow At the displayed
at>prompt, enter the command to execute and press Enter:batch
~]# batch at> sh /usr/local/bin/my-script.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow Repeat this step for every command you want to execute.
NoteThe
at>prompt shows which shell it will use:warning: commands will be executed using /bin/sh
warning: commands will be executed using /bin/shCopy to Clipboard Copied! Toggle word wrap Toggle overflow The batch utility uses the shell set in user’s SHELL environment variable, or the user’s login shell, or
/bin/sh, whichever is found first.- Press Ctrl+D on an empty line to finish specifying the job.
If the set of commands or the script tries to display information to standard output, the output is emailed to the user.
Changing the Default System Load Average Limit
By default, batch jobs start when system load average drops below 0.8. This setting is kept in the atq service. To change the system load limit:
To the
/etc/sysconfig/atdfile, add this line:OPTS='-l x'
OPTS='-l x'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Substitute x with the new load average. For example:
OPTS='-l 0.5'
OPTS='-l 0.5'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
atqservice:systemctl restart atq
# systemctl restart atqCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Viewing Pending Jobs
To view the list of pending jobs, use the atq command. See the section called “Viewing Pending Jobs”.
Deleting a Scheduled Job
To delete a scheduled job, use the atrm command. See the section called “Deleting a Scheduled Job”.
Controlling Access to Batch
You can also restrict the usage of the batch utility. This is done for the batch and at utilities together. See Section 24.3.2.1, “Controlling Access to At and Batch”.
24.5. Scheduling a Job to Run on Next Boot Using a systemd Unit File Copiar enlaceEnlace copiado en el portapapeles!
The cron, anacron, at, and batch utilities allow scheduling jobs for specific times or for when system workload reaches a certain level. It is also possible to create a job that will run during the next system boot. This is done by creating a systemd unit file that specifies the script to run and its dependencies.
To configure a script to run on the next boot:
Create the
systemdunit file that specifies at which stage of the boot process to run the script. This example shows a unit file with a reasonable set ofWants=andAfter=dependencies:Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you use this example:
-
substitute
/usr/local/bin/foobar.shwith the name of your script modify the set of
After=entries if necessaryFor information on specifying the stage of boot, see Section 10.6, “Creating and Modifying systemd Unit Files”.
-
substitute
If you want the
systemdservice to stay active after executing the script, add theRemainAfterExit=yesline to the[Service]section:[Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/local/bin/foobar.sh
[Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/local/bin/foobar.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow Reload the
systemddaemon:systemctl daemon-reload
~]# systemctl daemon-reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the
systemdservice:systemctl enable one-time.service
~]# systemctl enable one-time.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create the script to execute:
~]# cat /usr/local/bin/foobar.sh #!/bin/bash touch /root/test_file
~]# cat /usr/local/bin/foobar.sh #!/bin/bash touch /root/test_fileCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you want the script to run during the next boot only, and not on every boot, add a line that disables the
systemdunit:#!/bin/bash touch /root/test_file systemctl disable one-time.service
#!/bin/bash touch /root/test_file systemctl disable one-time.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Make the script executable:
chmod +x /usr/local/bin/foobar.sh
~]# chmod +x /usr/local/bin/foobar.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow
24.6. Additional Resources Copiar enlaceEnlace copiado en el portapapeles!
For more information on automating system tasks on Red Hat Enterprise Linux, see the resources listed below.
Installed Documentation
-
cron - The manual page for the crond daemon documents how
crondworks and how to change its behavior. - crontab - The manual page for the crontab utility provides a complete list of supported options.
-
crontab(5) - This section of the manual page for the crontab utility documents the format of
crontabfiles.