crontab

プロセスとシステム

書き方

crontab -e | crontab -l

利用者ごとの定期実行表を扱います。cron は PATH がほぼ空でログイン設定も読まないため絶対パスで書く必要があり、`crontab -r` は確認なしで表を丸ごと消します。

よく使うオプション

オプション意味
-eEdit your table in $EDITOR; it is installed when you save.
-lPrint your table. Redirect it somewhere to keep a backup.
-rDelete the whole table at once, with no confirmation.
-u <user>Work on another user's table (root only).
m h dom mon dowThe five time fields, then the command. * means every.
@reboot / @dailyShorthand instead of the five fields.

crontab -l > ~/cron.bak

Back up the table before touching it.

crontab -e

Then add: 0 3 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

crontab -l | grep -v backup.sh | crontab -

Remove one line without opening an editor.

何が動いていて何が資源を食っているかを見ます。止める前に何なのかを確かめるのが順番です。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. crontab は何をしますか。

利用者ごとの定期実行表を扱います。cron は PATH がほぼ空でログイン設定も読まないため絶対パスで書く必要があり、`crontab -r` は確認なしで表を丸ごと消します。

Q. どう打ちますか。

crontab -e | crontab -l — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは6個です。全部は man crontab にあります。このコマンドはプロセスとシステムの仲間です。

関連するコマンド

man ページ: man crontab