0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Linux】cronを設定する方法

Posted at

cron とは

cron を使用することで指定した時間や期間で自動的にコマンドを実行してくれます。

設定方法

cronの設定をするにはターミナルでcrontab -eを実行します。

ターミナル
crontab -e

するとエディタが開きますので設定ファイルを編集します。

このとき開くエディタは以下のようにEDITOR変数で設定できます。

.zshrc
export EDITOR=vim

設定ファイルは以下の形で記述します。

crontab〜
*  *  *  *  *  実行するコマンド

*の箇所にコマンドを実行する時間を記述します。

指定できる値は下記のようになります。

左から 分 時 日 月 曜 に対応します。
それぞれ以下の範囲を指定できます。

指定対象 指定範囲
0〜59
0〜23
1〜31
1〜12 または jan〜dec
曜日 0〜7 または sun〜sat

※曜日の0~7のうち0と7が日曜日になります。

ユーザーごとに設定でき、実行ユーザーは設定したユーザーになります。

他のユーザーの設定をしたい場合にはcrontab -eを実行する際にユーザーを指定します。

ターミナル
crontab -u ユーザー名 -e

またコマンドの実行場所は実行ユーザーのホームディレクトリになります。
ディレクトリ移動が必要な場合には以下のように記述して移動します。

* * * * * cd 移動先ディレクトリ; 実行するコマンド

cronでコマンドが実行されると実行ユーザーにコマンドの標準出力、標準エラー出力がメールされるようになっています。
メールの宛先を変更したい場合にはMAILTO変数で設定します。

crontab〜
MAILTO="sample@example.com"
*  *  *  *  *  実行するコマンド

またメールを送信しないでほしい場合には空にします。

crontab〜
MAILTO=""
*  *  *  *  *  実行するコマンド
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?