Skip to Content
ドキュメント運用システム管理スケジュールされたタスク

スケジュールされたタスク

スケジュールされたタスクを使用すると、指定した時間にコマンドまたはスクリプトを自動的に実行できます。 Ubuntu 26.04 は、従来の cron とより最新の systemd タイマーという 2 つの方法をサポートしています。

crontabの基本

crontabの管理

# 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 のヒント

環境変数

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=""

cron ログの表示

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

システムレベルの cron ディレクトリ

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 タイマー

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 ファイルの 2 つのファイルが必要です。

ステップ 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

一時的なタイマー (1 回限りの遅延実行)

# 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 タイマーの比較

特徴クロンシステムタイマー
構成の複雑さシンプルな一行2 つのファイルが必要です
ロギング手動セットアップが必要ですjournalctlとの統合
依存関係の管理サポートされていませんサービスの依存関係をサポートします
精度最小単位は分です秒単位まで正確
見逃したタスク未実行Persistent=true が追いつく
リソース制御サポートされていませんcgroup リソース制限をサポート
ランダムな遅延サポートされていませんRandomizedDelaySec

ベストプラクティス

  1. 単純なスケジュールされたタスクには cron を使用し、 複雑なタスクには systemd タイマーを使用します。
  2. トラブルシューティングを容易にするために、常に出力をログに記録してください。
  3. PATH の問題を回避するには、フルパスを使用してください。
  4. タスクの重複を防ぐ — 長時間実行されるタスクではロックを使用する必要があります。
# Use flock to prevent task overlap * * * * * /usr/bin/flock -n /tmp/myjob.lock /path/to/script.sh
  1. 展開前のテスト - 最初にスクリプトを手動で実行して、正しく動作することを確認します。