crontab 에서 사용하는 값 정리
OS/linux 2024. 4. 3. 23:39cron 은 주기적으로 특정 작업을 스케쥴링하기 위해 리눅스에세 제공해 주는 명령으로
contab 을 통해 작업들을 설정한다.
editor 설정
crontab -e 실행시 select-editor 명령이 호출되어 editor 설정된다.
editor를 바꾸고 싶을때는 select-editor 를 실행 하거나 ~/.selected_editor 의 SELECTED_EDITOR 의 값을 바꾼다.
select-editor 명령 실행시 원하는 editor가 없을 경우에는 SELECTED_EDITOR 값을 변경하자.
crontab -e 명령을 실행하면 다음과 같은 형식으로 값을 사용하면 된다.
m h dom mon dow command
field allowed values
----- --------------
minute 0–59
hour 0–23
day of month 1–31
month 1–12 (or names, see below)
day of week 0–7 (0 or 7 is Sun, or use names)
(Manual page crontab(5))
사용 예는 다음과 같다
The following lists an example of a user crontab file.
# use /bin/bash to run commands, instead of the default /bin/sh
SHELL=/bin/bash
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month — output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every Sunday"
0 */4 1 * mon echo "run every 4th hour on the 1st and on every Monday"
0 0 */2 * sun echo "run at midn on every Sunday that's an uneven date"
# Run on every second Saturday of the month
0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"
All the above examples run non-interactive programs. If you wish to run a program that interacts with the
user's desktop you have to make sure the proper environment variable DISPLAY is set.
# Execute a program and run a notification every day at 10:00 am
0 10 * * * $HOME/bin/program | DISPLAY=:0 notify-send "Program run" "$(cat)"
(Manual page crontab(5))
예약어도 사용할수 있다
string meaning
------ -------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".
(Manual page crontab(5))