66
69

More than 5 years have passed since last update.

定期的にスクリプトを自動実行させる

Last updated at Posted at 2017-05-10

cronというものを使います。以下の3ステップでできます。

1. 実行するスクリプトをつくります

ここでは myjob.shという名前でつくってみます

myjob.sh
echo "hello"

2. 自動実行ルールを書いたファイルをつくります

以下の書式に従って記述します

分 時 日 月 曜日 <実行コマンド>

ここでは毎時0分に実行するルールを cron.confという名前でつくってみます

cron.conf
0 * * * * /bin/bash myjob.sh

間隔指定・複数指定・範囲指定もできます

*/5 * * * * /bin/bash myjob.sh # 5分おきに実行
0,30 * * * * /bin/bash myjob.sh # 0分と30分に実行
3-5 * * * * /bin/bash myjob.sh # 3分、4分、5分に実行

3. cronにジョブとして登録します

$ crontab cron.conf

以下のコマンドで現在のジョブを確認できます。

$ crontab -l

0 * * * * /bin/bash myjob.sh

これで完了です。

66
69
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
66
69