crontab 명령어 (AIX 5.3·6.1)
https://archive.horanglab.com/posts/aix-cmd-crontab
핵심 정리
- cron 작업을 등록·편집(
-e)·나열(-l)·삭제(-r)·상태 확인(-v)합니다. 파일 이름을 주면 그 파일을 crontab으로 등록합니다. - crontab 파일은
/var/spool/cron/crontabs/사용자이름에 저장되며, 편집기는EDITOR변수(없으면vi)를 씁니다. - 항목 형식:
분 시 일 월 요일 명령— 분 059, 시 023, 일 131, 월 112, 요일 0~6(일요일 0).*,2-5,1,31,1,10-16,31을 쓸 수 있습니다.*/5같은 간격 표기는 문서에 없습니다. - 일과 요일을 둘 다 목록으로 지정하면 둘 중 하나만 맞아도 실행합니다. 명령 칸의
%는 표준 입력의 시작이며, 글자로 쓰려면\%로 씁니다. - 사용 권한은
/var/adm/cron/cron.allow와cron.deny가 정합니다. 둘 다 없으면 root만 쓸 수 있습니다. - cron이 주는 환경은
HOME,LOGNAME,SHELL=/usr/bin/sh,PATH=/usr/bin뿐이라.profile은 직접 읽어야 합니다.
| 항목 | 내용 |
|---|---|
| 버전 | AIX 5.3, AIX 6.1 (6.1에서 범위와 목록을 함께 쓰는 설명과 RBAC 문단이 더해짐) |
| 원문 | IBM Commands Reference — 5.3 Volume 1, 6.1 Volume 1 |
| 파일 | /var/spool/cron/crontabs/사용자, /var/adm/cron/cron.allow, /var/adm/cron/cron.deny |
| 관련 명령어 | cron 데몬, at, wall |
목적
cron 작업을 등록, 편집, 나열, 삭제합니다.
영어 원문
Submits, edits, lists, or removes cron jobs.
구문
crontab [ -e [ UserName ] | -l [ UserName ] | -r [ UserName ] | -v [ UserName ] | File ]
설명
crontab 명령은 cron 작업을 등록, 편집, 나열, 삭제합니다. cron 작업은 cron 데몬이 정해진 간격으로 실행하는 명령입니다. 작업을 등록하려면 crontab -e를 실행합니다. 편집 세션이 열려 crontab 파일을 만들 수 있으며, 작업마다 cron 데몬이 받아들이는 형식으로 항목을 씁니다(아래 "crontab 항목 형식" 참고).
항목을 다 쓰고 파일을 나가면 crontab이 그 파일을 /var/spool/cron/crontabs 디렉터리에 현재 사용자 이름으로 복사합니다. 같은 이름의 파일이 이미 있으면 덮어씁니다.
File 매개변수를 지정해 crontab 파일을 만들 수도 있습니다. 파일이 있으면 cron 데몬이 기대하는 형식이어야 합니다. 파일이 없으면 편집기를 엽니다. EDITOR 환경 변수가 있으면 그 편집기를, 없으면 vi를 씁니다.
crontab 파일의 내용을 보려면 -l을, 기존 파일을 지우려면 -r을 씁니다.
선택 매개변수 UserName은 crontab 파일의 소유자나 root 사용자가, 지정한 사용자의 cron 작업을 편집·나열·삭제·상태 확인할 때 씁니다. UserName이 올바르지 않으면 오류 메시지를 내고 끝냅니다. UserName을 지정하지 않으면 root 사용자와 현재 사용자가 플래그를 쓸 수 있습니다.
영어 원문
The crontab command submits, edits, lists, or removes cron jobs. A cron job is a command run by the cron daemon at regularly scheduled intervals. To submit a cron job, specify the crontab command with the -e flag. The crontab command invokes an editing session that allows you to create a crontab file. You create entries for each cron job in this file. Each entry must be in a form acceptable to the cron daemon. For information on creating entries, see The crontab File Entry Format.
When you finish creating entries and exit the file, the crontab command copies it into the /var/spool/cron/crontabs directory and places it in a file named for your current user name. If a file with your name already exists in the crontabs directory, the crontab command overwrites it.
Alternatively, you can create a crontab file by specifying the File parameter. If the file exists, it must be in the format the cron daemon expects. If the file does not exist, the crontab command invokes the editor. If the EDITOR environment variable exists, the command invokes the editor it specifies. Otherwise, the crontab command uses the vi editor.
To list the contents of your crontab file, specify the crontab command with the -l flag. To remove an existing file, use the -r flag.
The optional UserName parameter can be used by the owner of the crontab file or by the root user to edit, list, remove, or verify the status of the cron jobs for the specified user. If the UserName is invalid, an error message is generated and the program exits.
If the optional UserName parameter is not specified, the crontab flags are available for the root user and the current user.
보안
-e, -l, -r, -v 뒤에 UserName을 붙여 지정한 사용자의 crontab 파일을 다룰 수 있는 것은 root 사용자나 그 crontab 파일의 소유자뿐입니다.
cron 데몬
cron 데몬은 crontab 파일의 항목에 따라 명령을 실행합니다. 작업의 출력을 표준 출력이나 표준 오류로 재지정하지 않으면 명령의 출력이나 오류를 메일로 보내 줍니다. crontab 파일에 작업을 잘못 쓰면 그 작업은 실행하지 않습니다.
cron 데몬은 초기화할 때만 crontab 파일을 살펴봅니다. crontab 명령으로 파일을 바꾸면 바뀌었다는 메시지가 cron 데몬에 전달되므로, 데몬이 주기적으로 새 파일이나 바뀐 파일을 확인할 필요가 없습니다.
crontab 사용 제한
/var/adm/cron/cron.allow와 /var/adm/cron/cron.deny 파일이 crontab을 쓸 수 있는 사용자를 정합니다. root 사용자가 이 파일을 만들고 고치고 지웁니다. 파일의 항목은 한 줄에 하나씩 쓴 로그인 이름입니다. 로그인 ID에 로그인 이름이 여럿이면, 실제로 어떤 이름을 쓰든 /etc/passwd에서 처음 나오는 이름을 씁니다. 또 사용자가 cron 작업을 시작하려면 chuser 명령으로 /etc/security/user 파일의 daemon 속성을 TRUE로 둬야 합니다.
cron.allow 파일의 예:
root
nick
dee
sarah
cron.allow가 있으면 그 안에 로그인 이름이 있는 사용자만 쓸 수 있습니다. 이 경우 root의 로그인 이름도 그 파일에 있어야 합니다.- 관리자는
cron.deny에 사용자의 로그인 이름을 적어 그 사용자가 쓰지 못하게 할 수 있습니다.cron.deny만 있으면 거기에 없는 모든 사용자가 쓸 수 있습니다.
다음 중 하나라도 맞으면 사용자는 crontab을 쓸 수 없습니다.
cron.allow와cron.deny가 둘 다 없습니다(root만 허용).cron.allow가 있는데 사용자의 로그인 이름이 없습니다.cron.deny가 있고 사용자의 로그인 이름이 들어 있습니다.
crontab 항목 형식
crontab 파일은 cron 작업마다 항목을 하나씩 담으며, 항목은 줄바꿈으로 나뉩니다. 항목마다 빈칸이나 탭으로 나눈 여섯 필드가 다음 순서로 있습니다.
minute hour day_of_month month weekday command
| 필드 | 값 |
|---|---|
| minute | 0~59 |
| hour | 0~23 |
day_of_month |
1~31 |
| month | 1~12 |
| weekday | 06(일요일토요일) |
| command | 셸 명령 |
필드마다 값을 꼭 지정해야 합니다. command를 뺀 필드에는 다음을 쓸 수 있습니다.
| 형태 | 뜻과 예 |
|---|---|
| 범위 안의 숫자 | 5월에 실행하려면 month에 5 |
| 하이픈으로 이은 두 숫자 | 양끝 포함 범위. 화요일~금요일은 weekday에 2-5 |
| 쉼표로 나눈 숫자 목록 | 1월 1일과 마지막 날은 day_of_month에 1,31 |
| 범위와 목록을 함께 | 1일, 10~16일, 마지막 날은 day_of_month에 1,10-16,31(6.1 설명) |
*(별표) |
허용되는 모든 값. 매시간 실행하려면 hour에 * |
참고: 백슬래시를 앞에 붙인 문자(% 포함)는 글자 그대로 다룹니다. 날짜는 day_of_month와 weekday 두 필드로 지정할 수 있으며, 둘 다 목록으로 지정하면 둘 다 적용됩니다. 예를 들어 다음 항목은 매달 1일과 15일, 그리고 매주 월요일에 실행합니다. 한 필드로만 날짜를 정하려면 다른 필드에 *를 씁니다.
0 0 1,15 * 1 command
명령 지정
cron 데몬은 정한 날짜와 시각에 여섯째 필드의 명령을 실행합니다.
- 여섯째 필드에
%(퍼센트)가 있으면, 앞부분을 명령 호출로, 뒷부분을 표준 입력으로 줍니다.%를 글자로 쓰려면\%로 씁니다. - 빈 줄과, 공백이 아닌 첫 문자가
#인 줄은 무시합니다. - 명령의 인수에 백슬래시가 있으면 그 앞에 백슬래시를 하나 더 붙입니다.
- 셸은 명령 필드의 첫 줄만 실행하고, 나머지 줄은 명령의 표준 입력으로 줍니다.
- cron 데몬은 HOME 디렉터리에서 하위 셸을 시작합니다. 로그인하지 않은 때 실행되는 명령에서
.profile의 명령이 필요하면 명령이 직접.profile을 읽어야 합니다. - cron 데몬은 셸마다 기본 환경으로
HOME,LOGNAME,SHELL(=/usr/bin/sh),PATH(=/usr/bin)를 줍니다.
영어 원문
Only the root user or the owner of the crontab file can use UserName following the -e, -l, -r, and -v flags to edit, list, remove, or verify the crontab file of the specified user.
The cron Daemon
The cron daemon runs commands according to the crontab file entries. Unless you redirect the output of a cron job to standard output or error, the cron daemon mails you any command output or errors. If you specify a cron job incorrectly in your crontab file, the cron daemon does not run the job.
The cron daemon examines crontab files only when the cron daemon is initialized. When you make changes to your crontab file using the crontab command, a message indicating the change is sent to the cron daemon. This eliminates the overhead of checking for new or changed files at regularly scheduled intervals.
Controls on Using the crontab Command
The /var/adm/cron/cron.allow and /var/adm/cron/cron.deny files control which users can use the crontab command. A root user can create, edit, or delete these files. Entries in these files are user login names with one name to a line. If your login ID is associated with more than one login name, the crontab command uses the first login name that is in the /etc/passwd file, regardless of which login name you might actually be using. Also, to allow users to start cron jobs, the daemon attribute in the /etc/security/user file should be set to TRUE , using the chuser command.
The following is an example of an cron.allow file:
root nick dee sarah
If the cron.allow file exists, only users whose login names appear in it can use the crontab command. The root user's log name must appear in the cron.allow file if the file exists. A system administrator can explicitly stop a user from using the crontab command by listing the user's login name in the cron.deny file. If only the cron.deny file exists, any user whose name does not appear in the file can use the crontab command.
A user cannot use the crontab command if one of the following is true:
The cron.allow file and the cron.deny file do not exist (allows root user only).
The cron.allow file exists but the user's login name is not listed in it.
The cron.deny file exists and the user's login name is listed in it.
If neither the cron.allow nor the cron.deny file exists, only someone with root user authority can submit a job with the crontab command.
The crontab File Entry Format
A crontab file contains entries for each cron job. Entries are separated by newline characters. Each crontab file entry contains six fields separated by spaces or tabs in the following form:
minute hour day_of_month month weekday command
These fields accept the following values:
- minute — 0 through 59
- hour — 0 through 23
day_of_month— 1 through 31- month — 1 through 12
- weekday — 0 through 6 for Sunday through Saturday
- command — a shell command
You must specify a value for each field. Except for the command field, these fields can contain the following:
A number in the specified range. To run a command in May, specify 5 in the month field.
Two numbers separated by a dash to indicate an inclusive range. To run a cron job on Tuesday through Friday, place 2-5 in the weekday field.
A list of numbers separated by commas. To run a command on the first and last day of January, you would specify 1,31 in the
day_of_monthfield.A combination of two numbers separated by a dash to indicate an inclusive range and a list of numbers separated by commas can be used in conjunction. To run a command on the first, tenth to sixteenth and last day of January, you would specify 1,10-16,31 in the
day_of_monthfield. The above two points can also be used in combination.An
*(asterisk), meaning all allowed values. To run a job every hour, specify an asterisk in the hour field.
Note: Any character preceded by a backslash (including the %) causes that character to be treated literally. The specification of days may be made by two fields (day of the month and day of the week). If you specify both as a list of elements, both are adhered to. For example, the following entry:
0 0 1,15 * 1 command
would run command on the first and fifteenth days of each month, as well as every Monday. To specify days by only one field, the other field should contain an * .
Specifying Commands
The cron daemon runs the command named in the sixth field at the selected date and time. If you include a % (percent sign) in the sixth field, the cron daemon treats everything that precedes it as the command invocation and makes all that follows it available to standard input, unless you escape the percent sign (\%). Blank lines and lines whose first non-blank character is the number sign (#) will be ignored. If the arguments to the command have a backslash ('\'), the backslash should be preceded by another backslash.
Note: The shell runs only the first line of the command field. All other lines are made available to the command as standard input.
The cron daemon starts a subshell from your HOME directory. If you schedule a command to run when you are not logged in and you want commands in your .profile file to run, the command must explicitly read your .profile file.
The cron daemon supplies a default environment for every shell, defining HOME, LOGNAME, SHELL (=/usr/bin/sh), and PATH (=/usr/bin).
플래그
| 플래그 | 설명 |
|---|---|
-e UserName |
사용자의 crontab 파일 사본을 편집합니다. 올바른 UserName의 crontab 파일이 없으면 빈 파일을 만들어 편집합니다. 편집이 끝나면 그 사용자의 crontab 파일로 crontab 디렉터리에 복사합니다. |
-l UserName |
사용자의 crontab 파일을 나열합니다. |
-r UserName |
사용자의 crontab 파일을 crontab 디렉터리에서 지웁니다. |
-v UserName |
사용자의 cron 작업 상태를 나열합니다. |
영어 원문
-eUserName — Edits a copy of the user's crontab file or creates an empty file to edit if the crontab file does not exist for a valid UserName. When editing is complete, the file is copied into the crontab directory as the user's crontab file.-lUserName — Lists the user's crontab file.-rUserName — Removes the user's crontab file from the crontab directory.-vUserName — Lists the status of the user's cron jobs.
보안: 감사 이벤트
감사 서브시스템이 알맞게 설정되고 켜져 있으면, crontab은 실행할 때마다 다음 감사 레코드(이벤트)를 만듭니다.
| 이벤트 | 정보 |
|---|---|
CRON_JobRemove |
어느 사용자가 언제 crontab 파일을 지웠는지 |
CRON_JobAdd |
어느 사용자가 언제 crontab 파일을 편집했는지(5.3은 "cron 작업을 더했는지") |
RBAC 사용자와 Trusted AIX 사용자는 주의하세요. 이 명령은 특권 작업을 할 수 있으며, 특권 작업은 특권 사용자만 할 수 있습니다. 특권과 권한 목록은 lssecattr 명령이나 getcmdattr 하위 명령으로 봅니다. 이 문단은 6.1 문서에만 있습니다.
영어 원문
Auditing Events
If the auditing subsystem is properly configured and is enabled, the crontab command generates the following audit record (event) every time the command is run:
Event Information CRON_JobRemove Lists which users removed a crontab file and when. CRON_JobAdd Lists which users edited a crontab file and when.
Attention RBAC users and Trusted AIX users: This command can perform privileged operations. Only privileged users can run privileged operations. For more information about authorizations and privileges, see Privileged Command Database in Security. For a list of privileges and the authorizations associated with this command, see the lssecattr command or the getcmdattr subcommand.
종료 상태
| 값 | 뜻 |
|---|---|
| 0 | 잘 끝났습니다. |
| 0보다 큼 | 오류가 났습니다. |
영어 원문
This command returns the following exit values:
- 0 — Successful completion.
>0— An error occurred.
예제
mycronjobs파일을/var/spool/cron/crontabs디렉터리로 복사합니다. 파일은/var/spool/cron/crontabs/현재사용자이름으로 복사됩니다.
crontab mycronjobs
- 매시 정각에 콘솔에 시각을 씁니다(crontab 항목).
0 * * * * echo The hour is `date` . >/dev/console
- 매주 월·수·금요일 오전 6시 30분에
calendar를 실행합니다.
30 6 * * 1,3,5 /usr/bin/calendar
- 매일 6시 30분에
calendar를 실행합니다.
30 6 * * * /usr/bin/calendar
- 8월 한 달 동안 매일 자정에
maintenance스크립트를 실행합니다.
0 0 * 8 * /u/harry/bin/maintenance
- 명령의 표준 입력으로 줄 글을 정합니다. 12월의 금요일 16시에
wall을 실행하며,%뒤의 글이wall의 표준 입력이 됩니다(%마다 줄이 바뀜).
0 16 * 12 5 /usr/sbin/wall%HAPPY HOLIDAY!%Remember to turn in your time card.
wall이 받는 표준 입력:
HAPPY HOLIDAY!
Remember to turn in your time card.
영어 원문
- To copy a file called
mycronjobsinto the /var/spool/cron/crontabs directory, enter the following:crontab mycronjobs
The file will be copied as: /var/spool/cron/crontabs/<username>
where <username> is your current user name.
To write the time to the console every hour on the hour, enter:
0 * * * * echo The hour is 'date' . >/dev/consoleTo run the calendar command at 6:30 a.m. every Monday, Wednesday, and Friday, enter:
30 6 * * 1,3,5 /usr/bin/calendarTo run the calendar command every day of the year at 6:30, enter the following:
30 6 * * * /usr/bin/calendarTo run a script called
maintenanceevery day at midnight in August, enter the following:0 0 * 8 * /u/harry/bin/maintenanceTo define text for the standard input to a command, enter:
0 16 * 12 5 /usr/sbin/wall%HAPPY HOLIDAY!%Remember to turn in your time card.
The text following the % (percent sign) defines the standard input to the wall command as: HAPPY HOLIDAY!
Remember to turn in your time card.
파일
| 파일 | 설명 |
|---|---|
/var/adm/cron/FIFO |
crontab이나 at으로 새 작업을 등록할 때 cron 데몬에 메시지를 보내는 이름 있는 파이프입니다. |
/var/spool/cron/crontabs |
crontab 스풀 영역입니다. |
/var/adm/cron/cron.allow |
crontab을 쓸 수 있는 사용자 목록입니다. |
/var/adm/cron/cron.deny |
crontab을 쓸 수 없는 사용자 목록입니다. |
관련 명령어: cron 데몬, sh, wall
영어 원문
- /var/adm/cron/FIFO — A named pipe that sends messages to the cron daemon when new jobs are submitted with the crontab or at command.
- /var/spool/cron/crontabs — Specifies the crontab spool area.
- /var/adm/cron/cron.allow — Specifies a list of users allowed access to the crontab command.
- /var/adm/cron/cron.deny — Specifies a list of users denied access to the crontab command.
Related reference:
- cron Daemon
Related information:
- sh command
- wall command
- Auditing Overview
리눅스(GNU)와 다른 점
| 항목 | AIX 5.3·6.1 | 리눅스 cron(cronie, Vixie cron) |
|---|---|---|
| 간격 표기 | */5 없음(0,5,10,...처럼 나열) |
*/5 지원 |
| 이름 표기 | 숫자만(월 112, 요일 06) | jan, mon 같은 이름과 요일 7(일요일)도 받음 |
@reboot, @daily |
없음 | 있음 |
| 허용 파일 | /var/adm/cron/cron.allow, cron.deny |
/etc/cron.allow, /etc/cron.deny |
| 저장 위치 | /var/spool/cron/crontabs |
/var/spool/cron(배포판마다 다름) |
| 시스템 crontab | 없음(root의 crontab을 씀) | /etc/crontab, /etc/cron.d |
| 기본 PATH | /usr/bin |
/usr/bin:/bin |
리눅스의 crontab을 AIX로 옮길 때는 */5와 요일 이름을 숫자 목록으로 바꿔야 합니다.