LoginSignup
17
11

More than 3 years have passed since last update.

はじめてcronにタスクを登録する

Posted at
  • 環境
    • Debian GNU/Linux 10 (buster)
    • apt 1.8.2 (amd64)

RedmineようのGitリポジトリを定期的にfetchしたくて、はじめてcronでタスクを登録してみた

cronをaptでインストールする

# リポジトリ一覧を更新する
$ apt update
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
...省略...
48 packages can be upgraded. Run 'apt list --upgradable' to see them.
N: Repository 'http://deb.debian.org/debian buster InRelease' changed its 'Version' value from '10.0' to '10.3'

# cronを検索する
$ apt list cron
Listing... Done
cron/stable 3.0pl1-134+deb10u1 amd64

# cronをインストールする
$ apt install -y cron
Reading package lists... Done
Building dependency tree
...省略...
Processing triggers for mime-support (3.62) ...

# cron用のディレクトリを確認する
$ ls -la /etc/ | grep cron
drwxr-xr-x 2 root root      26 Apr  7 04:59 cron.d          # 下記以外の自動タスク設定ファイルを置くディレクトリ
drwxr-xr-x 1 root root      44 Apr  7 04:59 cron.daily      # 毎日実行される自動タスク設定ファイルを置くディレクトリ
drwxr-xr-x 2 root root      26 Apr  7 04:59 cron.hourly     # 毎時実行される自動タスク設定ファイルを置くディレクトリ
drwxr-xr-x 2 root root      26 Apr  7 04:59 cron.monthly    # 毎月実行される自動タスク設定ファイルを置くディレクトリ
drwxr-xr-x 2 root root      26 Apr  7 04:59 cron.weekly     # 毎週実行される自動タスク設定ファイルを置くディレクトリ
-rw-r--r-- 1 root root    1042 Oct 11 07:58 crontab         # 毎時、毎日、毎月、毎週の自動タスクのメイン設定ファイル

# cronの起動状態を確認する
$ /etc/init.d/cron status
[FAIL] cron is not running ... failed!

# cronをインストールしたばかりなのでタスクは何も登録されていない状態
$ crontab -l
no crontab for root

# cronを起動する
$ /etc/init.d/cron start
[ ok ] Starting periodic command scheduler: cron.

# cronの起動状態を確認する
$ /etc/init.d/cron status
[ ok ] cron is running.

cronにタスクを登録してシェルを定期的に実行できるようにする

# cron.dディレクトリにgit-cronという設定ファイルを作ってタスクを登録する
$ vi /etc/cron.d/git-cron
# 設定ファイルは最後に空行が必要らしい
$ cat /etc/cron.d/git-cron
*/5 * * * * root /path/to/git-fetch.sh >> /var/log/cron.log 2>&1


# 設定ファイルに実行権限をつける
$ chmod 755 /etc/cron.d/git-cron
$ ls -la /etc/cron.d/git-cron
-rwxr-xr-x 1 root root   51 Apr  7 05:46 git-cron

# 実行するシェルを作成する
$ vi /path/to/git-fetch.sh
# (本題ではない)内容はRedmineのGitを更新するというもの
$ cat /path/to/git-fetch.sh
#!/bin/sh
cd /path/to/hoge.git/
git fetch origin 'refs/heads/*:refs/heads/*'

cd /path/to/redmine
bundle exec rake redmine:fetch_changesets RAILS_ENV=production

# 実行するシェルに実行権限をつける
$ chmod 755 /path/to/git-fetch.sh
$ ls -la /path/to/git-fetch.sh
-rwxr-xr-x 1 root root 185 Apr  7 05:57 /path/to/git-fetch.sh

失敗したこと

/bin/sh: 1: /path/to/git-fetch.sh: Permission denied

  • 事象 : 設定ファイルで出力するようにしていたログにエラーが出ていた
  • 原因 : 実行対象のシェルに実行権限がないため
  • 対応 : シェルに実行権限をつける
$ ls -la /path/to/git-fetch.sh
-rw-r--r-- 1 root    root    185 Apr  7 05:57 git-fetch.sh
$ chmod 755 /path/to/git-fetch.sh
$ ls -la /path/to/git-fetch.sh
-rwxr-xr-x 1 root root 185 Apr  7 05:57 /path/to/git-fetch.sh

「5分毎に実行」と「毎時5分に実行」で書き方を間違えた

「5分毎に実行」したかったのに「毎時5分に実行」になっていた・・・

参考 : cronの設定方法 - Qiita

分 時 日 月 曜日 {実行コマンド}
# 毎時5分に実行
5 * * * * root {実行コマンド}
# 5分毎に実行
*/5 * * * * root {実行コマンド}
17
11
1

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
17
11