この記事を作った動機
cron を使ってみようと思って重い腰を持ち上げてみたら、意図したとおりにうまく動かせずにつまずいたのでその記録をとるだけ。うまくわかった気がしないし、間違いがあるかもしれないが、ご了承ください。
cron の構造
# 分 時 日(一か月単位) 月 日(一週間単位)
* * * * * /path/to/script.sh
確認事項
cron はインストールされているか?
yay -Qs cron
# pacman -Qs cron
# local/cronie 1.7.2-1
# Daemon that runs specified programs at scheduled times and related tools
エディターを設定したか?
デフォルトだと vi が利用されるようになっており、不便なことがあると思うので設定した方がいいかも。
# neovim -> "nvim" に設定
# visual studio code -> "code" に設定
export EDITOR=nvim
echo $EDITOR
# nvim
cron サービスが実行されているか?
- 確認
systemctl status cronie
# systemctl status crond
# ● cronie.service - Command Scheduler
# Loaded: loaded (/usr/lib/systemd/system/cronie.service; enabled; preset: disabled)
# Active: active (running) since Mon 2025-07-28 21:53:50 JST; 1h 7min ago
# Invocation: 748b761f9921445f8a759eb430f3507f
# Main PID: 354161 (crond)
# Tasks: 2 (limit: 629145)
# Memory: 1.3M (peak: 4.5M)
# CPU: 1.667s
# CGroup: /system.slice/cronie.service
# ├─354161 /usr/sbin/crond -n
# └─355485 /usr/sbin/anacron -s
#
# 7月 28 22:58:00 hostName CROND[361012]: (username) CMDEND (/path/to/script.sh)
# 7月 28 22:58:00 hostName CROND[361012]: pam_unix(crond:session): session closed for user username
# 7月 28 22:59:00 hostName crond[361097]: pam_unix(crond:session): session opened for user username(uid=1000) by username(uid=0)
# ...
- 有効化
systemctl enable --now cronie
# systemctl enable --now crond
例(crontab -l の内容)
毎分実行
* * * * * /path/to/script.sh
各時間 5 分目に実行
0時 5分に実行
1時 5分に実行
2時 5分に実行
…
5 * * * * /path/to/script.sh
毎日 5:00 ~ 5:59 に毎分実行
* 5 * * * /path/to/script.sh
毎月 5 日目に毎分実行
* * 5 * * /path/to/script.sh
5 月中に毎分実行
1月 | 2月 | 3月 | 4月 | 5月 | 6月 | 7月 | 8月 | 9月 | 10月 | 11月 | 12月 |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
* * * 5 * /path/to/script.sh
毎週 5 日目(金曜日)に毎分実行
日曜日 | 月曜日 | 火曜日 | 水曜日 | 木曜日 | 金曜日 | 土曜日 |
---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 |
* * * * 5 /path/to/script.sh
crontab.guru(おまけ)
なんか、cronの使い方について調べている過程で、GUIベースのcron管理ツールがあったので、そのインストールと起動方法について簡単に書くだけ。
インストール
curl https://crontab.guru/install | sh
ユーザ名とパスワードを設定
sudo cronitor dash --port 9000
# Dashboard credentials are not configured.
# Please set a username and password for dashboard access.
# These credentials will be saved to your configuration file and can be updated later from the Settings screen.
# Enter dashboard username: ユーザ名を入力
# Enter dashboard password: パスワードを入力
# Credentials saved to: /etc/cronitor/cronitor.json
# Please run 'cronitor dash' again to start the dashboard.
もう一回実行すると
sudo cronitor dash --port 9000
# デフォルトだと root ユーザの cron を見てしまうようなので、-u でユーザー名を指定するとより意図したとおりになるかも?
# sudo cronitor dash --port 9000 -u [username]
# Starting Cronitor dashboard on port 9000...
# Opening browser to http://localhost:9000...
# Dashboard running on http://localhost:9000 (Press Ctrl+C to stop)
ブラウザで開く
今回は、–port 9000 と指定したので、“http://localhost:9000"を、対象のcronが実行されているPC上で開く。basic 認証に設定しておいたユーザ名とパスワードを入れれば、以下のようにダッシュボードを利用できる。
関係のありそうなページ
参考にしたサイトとか
- Crontab.guru - The cron schedule expression generator
https://crontab.guru/ (2025年7月28日) - ChatGPT
https://chatgpt.com/ (2025年7月28日) - cron - ArchWiki
https://wiki.archlinux.org/title/Cron (2025年7月28日) - cron - What is this editor that gets opened by crontab? - Ask Ubuntu
https://askubuntu.com/questions/954750/what-is-this-editor-that-gets-opened-by-crontab (2025年7月28日)