🌟

【Linux】cron(クーロン)で30分ごとにジョブを実行する方法と登録例30個

に公開

Linux

Linuxのcron(クーロン)を使って、30分おきにスクリプトやコマンドを実行する設定方法を紹介します。


🕒 基本のcron設定(30分おき)

*/30 * * * * /path/to/your/command.sh

各フィールドの意味:

┌──────────── 分(0〜59)
│ ┌────────── 時(0〜23)
│ │ ┌──────── 日(1〜31)
│ │ │ ┌────── 月(1〜12)
│ │ │ │ ┌──── 曜日(0〜7)※0と7は日曜
│ │ │ │ │
*/30 * * * * ← 30分ごと

✅ 登録手順

  1. ターミナルで crontab -e を実行してエディタを開きます:
crontab -e
  1. 以下のようなcronジョブを追加します:
*/30 * * * * /home/user/scripts/run_task.sh >> /home/user/scripts/log.txt 2>&1
  • >> log.txt:出力をログに追記
  • 2>&1:標準エラーもログにまとめる

📦 使用例:30分ごとにバックアップ

*/30 * * * * /usr/bin/rsync -av /data /backup

💡 補足ポイント

  • cronの「*/30」は時刻基準で実行されます(例:12:00, 12:30, 13:00…)。
  • 「前回の実行から30分後」ではないので、実行開始タイミングに注意してください。

登録例

# cron 登録例30選

01. `0 9 * * * /path/to/script.sh  # 毎日午前9時に実行`
02. `0 0 * * 0 /path/to/script.sh  # 毎週日曜の深夜0時に実行`
03. `30 6 * * 1 /path/to/script.sh  # 毎週月曜の6:30に実行`
04. `0 12 1 * * /path/to/script.sh  # 毎月1日の正午に実行`
05. `0 0 1 1 * /path/to/script.sh  # 毎年1月1日の0:00に実行`
06. `*/15 * * * * /path/to/script.sh  # 15分ごとに実行`
07. `*/30 * * * * /path/to/script.sh  # 30分ごとに実行`
08. `0 */2 * * * /path/to/script.sh  # 2時間ごとに実行`
09. `0 0 */3 * * /path/to/script.sh  # 3日ごとに0時に実行`
10. `*/5 9-17 * * 1-5 /path/to/script.sh  # 平日の9〜17時の間に5分ごとに実行`
11. `0 10 * * 6 /path/to/script.sh  # 毎週土曜の10:00に実行`
12. `0 18 * * 5 /path/to/script.sh  # 毎週金曜の18:00に実行`
13. `0 8 * 6 * /path/to/script.sh  # 6月の毎朝8時に実行`
14. `0 0 29 2 * /path/to/script.sh  # うるう年以外はスキップ(2月29日)`
15. `0 0 1 */2 * /path/to/script.sh  # 偶数月の1日に実行`
16. `0 6,18 * * * /path/to/script.sh  # 毎日6時と18時に実行`
17. `0 9-17/2 * * * /path/to/script.sh  # 9〜17時の2時間おきに実行`
18. `0 22 * * 1-5 /path/to/script.sh  # 平日22時に実行`
19. `30 23 * * * /path/to/script.sh  # 毎晩23:30に実行`
20. `45 13 * * * /path/to/script.sh  # 毎日13:45に実行`
21. `*/10 0-3 * * * /path/to/script.sh  # 深夜0〜3時の間に10分ごとに実行`
22. `15 4 * * * /path/to/script.sh  # 毎朝4:15に実行`
23. `5 5 * * 1 /path/to/script.sh  # 毎週月曜5:05に実行`
24. `0 20 * * 2,4 /path/to/script.sh  # 火・木の20時に実行`
25. `0 */6 * * * /path/to/script.sh  # 6時間ごとに実行`
26. `0 10 15 3 * /path/to/script.sh  # 毎年3月15日10時に実行`
27. `0 10 * 12 0 /path/to/script.sh  # 12月の日曜日に10時に実行`
28. `0 8 1-7 * 1 /path/to/script.sh  # 各月の第1月曜日8時に実行(簡易)`
29. `0 9 1,15 * * /path/to/script.sh  # 毎月1日と15日9時に実行`
30. `0 7 * * 2-6 /path/to/script.sh  # 火〜土の7時に実行`

補足

・基本的には1分単位でしか設定できない。
・やろうと思えば sleep10秒 を実行前に入れることで00:00:10 とかに実行可能
(ただのエイヤーでやる時のみ、正確性を求めるならやめた方がいい)

Discussion