Documentation for a newer release is available. View Latest

시스템 작업 자동화

indexterm:[Automated Tasks] Tasks, also known as _jobs_, can be configured to run automatically within a specified period of time, on a specified date, or when the system load average decreases below 0.8.

Fedora is pre-configured to run important system tasks to keep the system updated. For example, the slocate database used by the locate command is updated daily. A system administrator can use automated tasks to perform periodic backups, monitor the system, run custom scripts, and so on.

Fedora comes with the following automated task utilities: cron, anacron, at, and batch.

Every utility is intended for scheduling a different job type: while Cron and Anacron schedule recurring jobs, At and Batch schedule one-time jobs (refer to Cron and Anacron and At and Batch respectively).

Fedora supports the use of systemd.timer for executing a job at a specific time. See man systemd.timer(5) for more information.

크론과 아나크론

Both Cron and Anacron can schedule execution of recurring tasks to a certain point in time defined by the exact time, day of the month, month, day of the week, and week.

Cron jobs can run as often as every minute. However, the utility assumes that the system is running continuously and if the system is not on at the time when a job is scheduled, the job is not executed.

On the other hand, Anacron remembers the scheduled jobs if the system is not running at the time when the job is scheduled. The job is then executed as soon as the system is up. However, Anacron can only run a job once a day. Also note that by default, Anacron only runs when your system is running on AC power and will not run if your system is being powered by a battery; this behavior is set up in the /etc/cron.hourly/0anacron script.

크론과 아나크론 설치

To install Cron and Anacron, you need to install the cronie package with Cron and the cronie-anacron package with Anacron (cronie-anacron is a sub-package of cronie).

만약 꾸러미가 이미 시스템에 설치되어 있는지 확인하려면, 다음 명령을 실행하세요:

rpm -q cronie cronie-anacron

The command returns full names of the cronie and cronie-anacron packages if already installed, or notifies you that the packages are not available.

이들 꾸러미를 설치 할 때에, `root`으로 다음 형식에서 [command]#dnf#를 사용하세요:

dnf install package

예를 들면, Cron과 Anacron을 함께 설치하려면, 쉘 입력기에서 다음과 같이 입력하세요:

~]# dnf install cronie cronie-anacron

{MAJORS}에서 신규 꾸러미를 설치하는 방법에 대한 더 많은 정보를 위해, 꾸러미 설치하기를 참조하세요.

Crond 서비스 동작 중

`crond`서비스에 의해 cron과 anacron 작업은 모두 선택됩니다. 이 부분에서는 'crond' 서비스를 시작, 중지 그리고 재시작 하는 방법에 대한 정보를 제공하고, 부트시에 자동으로 시작하도록 구성하는 방법을 보여줍니다.

Cron 서비스 시작과 멈춤

만약 서비스를 동작하기로 결정하려면, 다음 명령을 사용하세요:

systemctl status crond.service

현재 세션에서 crond 서비스를 동작하려면, `root`으로 쉘 입력기에 다음을 입력하세요:

systemctl start crond.service

부트 시에 자동으로 시작하려는 서비스를 구성하려면, `root`으로 다음 명령을 사용하세요:

systemctl enable crond.service

크론 서비스 중지

현재 세션에서 `crond`서비스를 멈추려면, `root`으로 쉘 입력기에 다음과 같이 입력하세요:

systemctl stop crond.service

부트 시에 자동으로 시작 하는 서비스를 방지하려면, `root`으로 다음 명령을 사용하세요:

systemctl disable crond.service

크론 서비스 재시작하기

crond 서비스를 재시작 하려면, `root`으로 쉘 입력기에 다음과 같이 입력하세요:

systemctl restart crond.service

이 명령은 서비스를 멈추고 빠르게 연속으로 다시 시작합니다.

아나크론 작업 구성하기

작업 계획에서 주요 구성 파일은 root 사용자에 의해서 단지 접근 되어 질 수 있는 `/etc/anacrontab`입니다. 파일은 다음과 같이 포함되어 있습니다:

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1         5     cron.daily    nice run-parts /etc/cron.daily
7         25    cron.weekly   nice run-parts /etc/cron.weekly
@monthly  45    cron.monthly  nice run-parts /etc/cron.monthly

처음 세 줄은 아나크론 작업이 실행되는 환경을 구성하는 변수를 정의합니다:

  • SHELL — 동작 작업을 위해 사용되어지는 쉘 환경 (예로써, 배쉬 쉘)

  • 경로 — 실행 가능한 프로그램에서 경로

  • MAILTO — 전자우편에 의해 아나크론의 출력을 수신하는 사용자의 사용자이름

    만약 MAILTO 변수가 (MAILTO=)로 정의되지 않으면, 전자우편은 발송되지 않습니다.

다음 두 변수는 정의된 작업을 위해 예정된 시간을 수정합니다:

  • RANDOM_DELAY — maximum number of minutes that will be added to the delay in minutes variable which is specified for each job

    최소 지연 값은 기본적으로 6분으로 설정됩니다.

    If RANDOM_DELAY is, for example, set to 12, then between 6 and 12 minutes are added to the delay in minutes for each job in that particular anacrontab. RANDOM_DELAY can also be set to a value below 6, including 0. When set to 0, no random delay is added. This proves to be useful when, for example, more computers that share one network connection need to download the same data every day.

  • START_HOURS_RANGE — interval, when scheduled jobs can be run, in hours

    In case the time interval is missed, for example due to a power failure, the scheduled jobs are not executed that day.

The remaining lines in the /etc/anacrontab file represent scheduled jobs and follow this format:

period in days   delay in minutes   job-identifier   command
  • period in days — frequency of job execution in days

    The property value can be defined as an integer or a macro (@daily, @weekly, @monthly), where @daily denotes the same value as integer 1, @weekly the same as 7, and @monthly specifies that the job is run once a month regardless of the length of the month.

  • 지연(분) — 아나크론이 작업을 실행하기 전에 대기하는 시간(분)

    속성 값은 정수로써 정의되었습니다. 만약 값이 `0`으로 설정되면, 지연 없이 적용됩니다.

  • job-identifier — unique name referring to a particular job used in the log files

  • command — 실행 할 때의 명령

    The command can be either a command such as ls /proc >> /tmp/proc or a command which executes a custom script.

해시 기호 (#)로 시작하는 모든 행은 주석이고 동작하지 않습니다.

아나크론 작업의 예제

다음 예제는 단순한 /etc/anacrontab 파일을 보여줍니다:

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 작업의 기본 지연에 추가된 최대 임의 지연
RANDOM_DELAY=30
# 작업은 다음 시간에서만 시작됩니다
START_HOURS_RANGE=16-20

#period in days   delay in minutes   job-identifier   command
1         20    dailyjob      nice run-parts /etc/cron.daily
7         25    weeklyjob     /etc/weeklyjob.bash
@monthly  45    monthlyjob    ls /proc >> /tmp/proc

이와 같은 anacrontab 파일에서 정의된 모든 작업은 임의로 6-30분 지연되고 16시에서 20시 사이에 실행 될 수 있습니다.

The first defined job is triggered daily between 16:26 and 16:50 (RANDOM_DELAY is between 6 and 30 minutes; the delay in minutes property adds 20 minutes). The command specified for this job executes all present programs in the /etc/cron.daily/ directory using the run-parts script (the run-parts scripts accepts a directory as a command-line argument and sequentially executes every program in the directory). See the run-parts man page for more information on the run-parts script.

두 번째 작업은 일주일에 한번 /etc/ 디렉토리에서 weeklyjob.bash 스크립트를 실행합니다.

The third job runs a command, which writes the contents of /proc to the /tmp/proc file (ls /proc >> /tmp/proc) once a month.

크론 작업 구성하기

The configuration file for cron jobs is /etc/crontab, which can be only modified by the root user. The file contains the following:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# 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

The first three lines contain the same variable definitions as an anacrontab file: SHELL, PATH, and MAILTO. For more information about these variables, see Configuring Anacron Jobs.

In addition, the file can define the HOME variable. The HOME variable defines the directory, which will be used as the home directory when executing commands or scripts run by the job.

The remaining lines in the /etc/crontab file represent scheduled jobs and have the following format:

minute   hour   day   month   day of week   username   command

다음은 작업을 동작 할 때에 시간을 정의합니다:

  • minute — 0에서 59까지의 임의 정수

  • hour — 0에서 23까지의 임의 정수

  • day — 1에서 31까지의 임의 정수 (만약 월이 지정되었으면 유효한 날짜이어야만 합니다)

  • month — 1에서 12까지의 임의 정수 (또는 jan 또는 feb와 같이 월의 약어)

  • day of week — any integer from 0 to 7, where 0 or 7 represents Sunday (or the short name of the week such as sun or mon)

다음은 다른 작업 특성을 설명합니다:

  • username — 작업이 동작 하는 데에 사용자를 지정합니다.

  • command — 실행 할 명령입니다.

    The command can be either a command such as ls /proc /tmp/proc or a command which executes a custom script.

For any of the above values, an asterisk (*) can be used to specify all valid values. If you, for example, define the month value as an asterisk, the job will be executed every month within the constraints of the other values.

A hyphen (-) between integers specifies a range of integers. For example, 1-4 means the integers 1, 2, 3, and 4.

A list of values separated by commas (,) specifies a list. For example, 3,4,6,8 indicates exactly these four integers.

The forward slash (/) can be used to specify step values. The value of an integer will be skipped within a range following the range with /integer. For example, the minute value defined as 0-59/2 denotes every other minute in the minute field. Step values can also be used with an asterisk. For instance, if the month value is defined as */3, the task will run every third month.

해시 기호 (#)로 시작하는 모든 행은 주석이고 동작하지 않습니다.

Users other than root can configure cron tasks with the crontab utility. The user-defined crontabs are stored in the /var/spool/cron/ directory and executed as if run by the users that created them.

To create a crontab as a specific user, login as that user and type the command crontab -e to edit the user’s crontab with the editor specified in the VISUAL or EDITOR environment variable. The file uses the same format as /etc/crontab. When the changes to the crontab are saved, the crontab is stored according to the user name and written to the file /var/spool/cron/username. To list the contents of the current user’s crontab file, use the crontab -l command.

The /etc/cron.d/ directory contains files that have the same syntax as the /etc/crontab file. Only root is allowed to create and modify files in this directory.

변경을 적용 할 때에 데몬을 재시작 하지 마세요

The cron daemon checks the /etc/anacrontab file, the /etc/crontab file, the /etc/cron.d/ directory, and the /var/spool/cron/ directory every minute for changes and the detected changes are loaded into memory. It is therefore not necessary to restart the daemon after an anacrontab or a crontab file have been changed.

크론에 대한 접근 제어하기

To restrict the access to Cron, you can use the /etc/cron.allow and /etc/cron.deny files. These access control files use the same format with one user name on each line. Mind that no whitespace characters are permitted in either file.

If the cron.allow file exists, only users listed in the file are allowed to use cron, and the cron.deny file is ignored.

If the cron.allow file does not exist, users listed in the cron.deny file are not allowed to use Cron.

The Cron daemon (crond) does not have to be restarted if the access control files are modified. The access control files are checked each time a user tries to add or delete a cron job.

The root user can always use cron, regardless of the user names listed in the access control files.

You can control the access also through Pluggable Authentication Modules (PAM). The settings are stored in the /etc/security/access.conf file. For example, after adding the following line to the file, no other user but the root user can create crontabs:

-:ALL EXCEPT root :cron

The forbidden jobs are logged in an appropriate log file or, when using crontab -e, returned to the standard output. For more information, see the access.conf.5 manual page.

크론 작업의 블랙과 화이트 목록

Black and white listing of jobs is used to define parts of a job that do not need to be executed. This is useful when calling the run-parts script on a Cron directory, such as /etc/cron.daily/: if the user adds programs located in the directory to the job black list, the run-parts script will not execute these programs.

To define a black list, create a jobs.deny file in the directory that run-parts scripts will be executing from. For example, if you need to omit a particular program from /etc/cron.daily/, create the /etc/cron.daily/jobs.deny file. In this file, specify the names of the programs to be omitted from execution (only programs located in the same directory can be enlisted). If a job runs a command which runs the programs from the /etc/cron.daily/ directory, such as run-parts /etc/cron.daily, the programs defined in the jobs.deny file will not be executed.

화이트 목록을 정의하려면, jobs.allow 파일을 생성합니다.

The principles of jobs.deny and jobs.allow are the same as those of cron.deny and cron.allow described in section Controlling Access to Cron.

At과 Batch

While Cron is used to schedule recurring tasks, the At utility is used to schedule a one-time task at a specific time and the Batch utility is used to schedule a one-time task to be executed when the system load average drops below 0.8.

at과 batch 설치

만약 at 꾸러미가 이미 자신의 시스템에 설치되어 있는지 확인하려면, 다음 명령을 실행하세요:

rpm -q at

The command returns the full name of the at package if already installed or notifies you that the package is not available.

이들 꾸러미를 설치 할 때에, `root`으로 다음 형식에서 [command]#dnf#를 사용하세요:

dnf install package

예를 들면, At 과 Batch를 함께 설치하려면, 쉘 입력기에서 다음과 같이 입력하세요:

~]# dnf install at

{MAJORS}에서 신규 꾸러미를 설치하는 방법에 대한 더 많은 정보를 위해, 꾸러미 설치하기를 참조하세요.

at 서비스에서 동작 중

`atd`서비스에 의해 At과 Batch 작업은 모두 선택됩니다. 이 부분에서는 'atd' 서비스를 시작, 중지 그리고 재시작 하는 방법에 대한 정보를 제공하고, 부트시에 자동으로 시작하도록 구성하는 방법을 보여줍니다.

At 서비스 시작과 멈춤

만약 서비스를 동작하기로 결정하려면, 다음 명령을 사용하세요:

systemctl status atd.service

현재 세션에서 `atd`서비스를 실행하려면, `root`로서 쉡 입력기에서 다음과 같이 입력하세요:

systemctl enable atd.service

부트 시에 자동으로 시작하려는 서비스를 구성하려면, `root`으로 다음 명령을 사용하세요:

systemctl enable atd.service

It is recommended that you configure your system to start the atd service automatically at boot time.

서비스 중지

atd 서비스를 중지하려면, `root`로서 쉘 입력기에서 다음과 같이 입력하세요:

systemctl enable atd.service

부트 시에 자동으로 시작 하는 서비스를 방지하려면, `root`으로 다음 명령을 사용하세요:

systemctl disable atd.service

at 서비스 재시작하기

atd 서비스를 재시작 하려면, `root`로서 쉘 입력기에서 다음과 같이 입력하세요:

systemctl enable atd.service

이 명령은 서비스를 멈추고 빠르게 연속으로 다시 시작합니다.

At 작업 구성하기

To schedule a one-time job for a specific time with the At utility, do the following:

  1. On the command line, type the command at TIME, where TIME is the time when the command is to be executed.

    The TIME argument can be defined in any of the following formats:

    • HH:MM specifies the exact hour and minute; For example, 04:00 specifies 4:00 a.m.

    • 한밤중 오전 12:00로 정의합니다.

    • `정오`는 12:00 P.M.으로 정의합니다.

    • 새참시간 오후 4:00로 정의합니다.

    • MONTHDAYYEAR format; For example, January 15 2012 specifies the 15th day of January in the year 2012. The year value is optional.

    • MMDDYY, MM/DD/YY, or MM.DD.YY formats; For example, 011512 for the 15th day of January in the year 2012.

    • now + TIME where TIME is defined as an integer and the value type: minutes, hours, days, or weeks. For example, now + 5 days specifies that the command will be executed at the same time five days from now.

      The time must be specified first, followed by the optional date. For more information about the time format, see the /usr/share/doc/at-<version>/timespec text file.

      If the specified time has past, the job is executed at the time the next day.

  2. In the displayed at> prompt, define the job commands:

    1. Type the command the job should execute and press Enter. Optionally, repeat the step to provide multiple commands.

    2. Enter a shell script at the prompt and press Enter after each line in the script.

      The job will use the shell set in the user’s SHELL environment, the user’s login shell, or /bin/sh (whichever is found first).

  3. Once finished, press Ctrl+D on an empty line to exit the prompt.

If the set of commands or the script tries to display information to standard output, the output is emailed to the user.

To view the list of pending jobs, use the atq command. See Viewing Pending Jobs for more information.

You can also restrict the usage of the at command. For more information, see Controlling Access to At and Batch for details.

Batch 작업 구성하기

The Batch application executes the defined one-time tasks when the system load average decreases below 0.8.

Batch 작업을 정의하려면, 다음과 같이 실행하세요:

  1. 명령 줄에서, 명령 batch 를 입력하세요.

  2. In the displayed at> prompt, define the job commands:

    1. Type the command the job should execute and press Enter. Optionally, repeat the step to provide multiple commands.

    2. Enter a shell script at the prompt and press Enter after each line in the script.

      If a script is entered, the job uses the shell set in the user’s SHELL environment, the user’s login shell, or /bin/sh (whichever is found first).

  3. Once finished, press Ctrl+D on an empty line to exit the prompt.

If the set of commands or the script tries to display information to standard output, the output is emailed to the user.

To view the list of pending jobs, use the atq command. See Viewing Pending Jobs for more information.

You can also restrict the usage of the batch command. For more information, see Controlling Access to At and Batch for details.

중지 작업 보기

To view the pending At and Batch jobs, run the atq command. The atq command displays a list of pending jobs, with each job on a separate line. Each line follows the job number, date, hour, job class, and user name format. Users can only view their own jobs. If the root user executes the atq command, all jobs for all users are displayed.

추가 명령 줄 옵션

다음에 포함되어 있는 atbatch 를 위한 추가 명령 줄 선택:

Table 1. atbatch 명령 줄 선택
Option Description

-f

Read the commands or shell script from a file instead of specifying them at the prompt.

-m

Send email to the user when the job has been completed.

-v

Display the time that the job is executed.

at과 batch 접근 제어하기

You can restrict the access to the at and batch commands using the /etc/at.allow and /etc/at.deny files. These access control files use the same format defining one user name on each line. Mind that no whitespace are permitted in either file.

If the file at.allow exists, only users listed in the file are allowed to use at or batch, and the at.deny file is ignored.

If at.allow does not exist, users listed in at.deny are not allowed to use at or batch.

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.

The root user can always execute at and batch commands, regardless of the content of the access control files.

추가 자원

To learn more about configuring automated tasks, see the following installed documentation:

  • cron(8) man page contains an overview of cron.

  • crontab 섹션 1과 5에서 맨 해당 부분:

    • 섹션 1에 있는 설명서 부분은 crontab 파일의 개요를 포함합니다.

    • 섹션 5에 있는 맨 부분은 파일과 몇개의 예제 항목을 위한 형식을 포함하고 있습니다.

  • anacron(8) 설명서 부분은 아나크론의 개요를 포함합니다.

  • anacrontab(5)`설명서 부분은 `anacrontab 파일의 개요를 포함합니다.

  • run-parts(4) manual page contains an overview of the run-parts script.

  • /usr/share/doc/at/timespec contains detailed information about the time values that can be used in cron job definitions.

  • at manual page contains descriptions of at and batch and their command line options.