Skip to Content
문서운영시스템 관리예약된 작업

예약된 작업

예약된 작업을 사용하면 지정된 시간에 명령이나 스크립트를 자동으로 실행할 수 있습니다. Ubuntu 26.04는 전통적인 cron과 보다 현대적인 시스템 타이머라는 두 가지 방법을 지원합니다.

크론탭 기본 사항

크론탭 관리

# Edit the current user's crontab crontab -e # View the current user's crontab crontab -l # Delete the current user's crontab crontab -r # Edit another user's crontab (requires root) sudo crontab -u username -e # View another user's crontab sudo crontab -u username -l

cron 시간 구문

cron 표현식은 5개의 시간 필드로 구성됩니다.

┌──────── Minute (0-59) │ ┌────── Hour (0-23) │ │ ┌──── Day of month (1-31) │ │ │ ┌── Month (1-12) │ │ │ │ ┌ Day of week (0-7, both 0 and 7 mean Sunday) │ │ │ │ │ * * * * * command

특수 문자

캐릭터설명예
*모든 값* * * * * 매분
,다중 값1분, 15분, 30분에 1,15,30 * * * *
-범위1-5 * * * *분 1~5
/단계10분마다 */10 * * * *

일반적인 예

# Run every minute * * * * * /path/to/script.sh # Run every hour (on the hour) 0 * * * * /path/to/script.sh # Run daily at 3:00 AM 0 3 * * * /path/to/script.sh # Run daily at 3:30 AM 30 3 * * * /path/to/script.sh # Run every Monday at 2:00 AM 0 2 * * 1 /path/to/script.sh # Run on the 1st of every month at midnight 0 0 1 * * /path/to/script.sh # Run every 5 minutes */5 * * * * /path/to/script.sh # Run at minutes 15 and 45 of every hour 15,45 * * * * /path/to/script.sh # Run weekdays (Monday-Friday) at 9:00 AM 0 9 * * 1-5 /path/to/script.sh # Run every hour from 8 AM to 6 PM daily 0 8-18 * * * /path/to/script.sh # Run every 6 months (1st of January and July) 0 0 1 1,7 * /path/to/script.sh

특수 키워드

# Equivalent shortcuts @reboot # Run at system startup @yearly # Run yearly = 0 0 1 1 * @monthly # Run monthly = 0 0 1 * * @weekly # Run weekly = 0 0 * * 0 @daily # Run daily = 0 0 * * * @hourly # Run hourly = 0 * * * * # Usage examples @reboot /opt/myapp/start.sh @daily /usr/local/bin/daily-backup.sh

실용적인 크론 팁

환경 변수

cron 실행 환경은 사용자의 셸 환경과 다릅니다. PATH와 같은 변수는 불완전할 수 있습니다.

# Set environment variables at the top of your crontab SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin MAILTO=admin@example.com # Or use full paths in commands 0 3 * * * /usr/bin/rsync -av /source/ /backup/

로깅

# Redirect output to a log file 0 3 * * * /path/to/script.sh >> /var/log/myscript.log 2>&1 # Discard all output 0 3 * * * /path/to/script.sh > /dev/null 2>&1 # Log only errors 0 3 * * * /path/to/script.sh > /dev/null 2>> /var/log/myscript-error.log

이메일 알림

# Set the email recipient in crontab MAILTO=admin@example.com # Disable email notifications MAILTO=""

크론 로그 보기

# View via journalctl journalctl -u cron.service # Or check syslog grep CRON /var/log/syslog

시스템 수준 크론 디렉터리

Ubuntu는 미리 정의된 여러 cron 디렉터리를 제공합니다.

# Place scripts in the appropriate directory for automatic execution ls /etc/cron.hourly/ # Hourly ls /etc/cron.daily/ # Daily ls /etc/cron.weekly/ # Weekly ls /etc/cron.monthly/ # Monthly
# Add a script and ensure it has execute permission sudo cp myscript.sh /etc/cron.daily/ sudo chmod +x /etc/cron.daily/myscript.sh

시스템 수준 cron 구성 파일 /etc/crontab:

cat /etc/crontab
SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

시스템 타이머

systemd 타이머는 cron의 현대적인 대안으로 보다 정확한 시간 제어와 더 나은 로깅 지원을 제공합니다.

기존 타이머 보기

# List all active timers systemctl list-timers --all # Example output: # NEXT LEFT LAST PASSED UNIT ACTIVATES # Tue 2026-03-24 06:00:00 CST 2h left Mon 2026-03-23 06:00:00 CST 22h ago apt-daily.timer apt-daily.service

맞춤 타이머 만들기

타이머를 생성하려면 .service 파일과 .timer 파일이라는 두 개의 파일이 필요합니다.

1단계: 서비스 파일 생성

sudo nano /etc/systemd/system/my-backup.service
[Unit] Description=Daily Backup Job [Service] Type=oneshot ExecStart=/usr/local/bin/daily-backup.sh User=root StandardOutput=journal StandardError=journal

2단계: 타이머 파일 만들기

sudo nano /etc/systemd/system/my-backup.timer
[Unit] Description=Run Daily Backup at 3 AM [Timer] OnCalendar=*-*-* 03:00:00 Persistent=true RandomizedDelaySec=300 [Install] WantedBy=timers.target

3단계: 타이머 활성화

sudo systemctl daemon-reload sudo systemctl enable --now my-backup.timer # Verify systemctl list-timers | grep my-backup # Manually trigger a test run sudo systemctl start my-backup.service # View execution logs journalctl -u my-backup.service

OnCalendar 시간 표현

# Daily at 3 AM OnCalendar=*-*-* 03:00:00 # Every Monday at 9 AM OnCalendar=Mon *-*-* 09:00:00 # 1st of every month OnCalendar=*-*-01 00:00:00 # Weekdays at 8 AM OnCalendar=Mon..Fri *-*-* 08:00:00 # Every 15 minutes OnCalendar=*:0/15 # Every hour OnCalendar=hourly # Every day OnCalendar=daily # Every week OnCalendar=weekly

시간 표현을 확인합니다.

# Test an expression and see upcoming trigger times systemd-analyze calendar "Mon..Fri *-*-* 08:00:00" systemd-analyze calendar "*:0/15"

이벤트 기반 타이머

달력 기반 예약 외에도 이벤트를 기반으로 트리거할 수 있습니다.

[Timer] # Run 5 minutes after system boot OnBootSec=5min # Run 1 hour after the last execution completed OnUnitActiveSec=1h # Combined usage OnBootSec=10min OnUnitActiveSec=1h

과도 타이머(일회성 지연 실행)

# Run a command after 30 minutes systemd-run --on-active=30m /usr/local/bin/task.sh # Run at a specific time systemd-run --on-calendar="2026-03-25 10:00:00" /usr/local/bin/task.sh

cron과 systemd 타이머 비교

특징크론시스템 타이머
구성 복잡성간단하고 한 줄두 개의 파일이 필요합니다
로깅수동 설정 필요Journalctl과 통합
종속성 관리지원되지 않음서비스 종속성을 지원합니다.
정밀도최소 단위는 분입니다.초 단위까지 정확함
놓친 작업실행되지 않음Percious=true가 따라잡습니다.
자원 제어지원되지 않음cgroup 리소스 제한 지원
무작위 지연지원되지 않음무작위 지연초

모범 사례

  1. 간단한 예약 작업에는 cron을 사용하고 복잡한 작업에는 시스템 타이머를 사용하세요.
  2. 항상 출력을 기록하면 문제 해결이 더 쉬워집니다.
  3. 전체 경로를 사용하여 PATH 문제를 방지하세요.
  4. 작업 중복 방지 — 장기 실행 작업은 잠금을 사용해야 합니다.
# Use flock to prevent task overlap * * * * * /usr/bin/flock -n /tmp/myjob.lock /path/to/script.sh
  1. 배포 전 테스트 — 먼저 스크립트를 수동으로 실행하여 올바르게 작동하는지 확인하세요.