Overview
テスト実装を定期的に回すためにcron設定をします。
Input
- CentOS6以降 での cron の設定
- Cronの使い方とテクニックと詰まったところ
- cron を10分おき 2時間おきに実行する方法
- CronでPythonのモジュールがImportError: No moduleになる
Proccess
cron インストール
$sudo yum -y install cronie-noanacron $sudo yum remove cronie-anacron $sudo vim /etc/crontab
設定ファイルの配置
$ sudo cp /etc/crontab /etc/cron.d/auto_test
設定ファイルの編集
Pythonは複数入れているのでフルパスでないと動かない。。。
# which python /usr/local/pyenv/shims/python
$ sudo vi /etc/cron.d/auto_test ~~~~~ SHELL=/bin/bash PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed */5 * * * * root /usr/local/pyenv/shims/python /xxxxx/xxxxx/xxxxx/xxxxx.py
起動
sudo /etc/init.d/crond start
sudo cat /var/log/cron ~~~~~~~ (root) FAILED to authorize user with PAM (Module is unknown)
うごいてない。。。
sudo /etc/init.d/crond restart
動いた。 (実際はエラーが発生してまたプチハマり)
Output
動いてくれた。。。
今回もプチハマり。
Traceback (most recent call last): File "/xxxxx/xxxxx/xxxxx/xxxxx.py", line 2, in <module> import requests ImportError: No module named requests
複数バージョン入れてるのでcronのPythonはフルパスで指定。