- 追加された行はこの色です。
- 削除された行はこの色です。
[[FrontPage]]
* Ubuntu [#tdcc2daa]
* Virtual Box [#m2a07013]
** 共有フォルダ [#l42adc95]
# Windows Share Dr
alias share='cd $SHARE'
sudo mount.vboxsf share ~/share
export SHARE=~/share/
* python path [#qd42f416]
** python path [#qd42f416]
$ which python
/usr/bin/python
* cron [#o945ca7c]
** cron [#o945ca7c]
http://qiita.com/gano/items/802519add83c524e3019
テストスクリプト
crontest.py
# -*- coding: utf-8 -*-
import sys
with open("hoge.txt", "w") as f:
f.write("hello python")
f.write(str(sys.version))
-cronの編集
crontab -e (編集)
crontab -r (リセットされる)
-cronの安全な編集
crontab -l > ~/crontab #バックアップ
vim ~/crontab #バックアップを編集
crontab < ~/crontab #本体に上書き
-crontabへの書き込み
* * * * * /<path_to_python>/python3 /<path_to_file>/test.py
* * * * * <path_to_python>/python3 /<path_to_file/test.py >> /<path_to_log>/test.log 2>>&1
-pythonパスの確認
※ python path
which python3
-実行権限付加
chmod 700 test.py
chown root:root test.py
- (エラー)
(CRON) info (No MTA installed, discarding output)
解決
$ sudo apt-get install postfix
- cronの再起動
sudo service cron status
sudo service cron start
sudo service cron start
-実行ログ
/var/log/syslog
** 結局 [#q715a499]
/etc/cron.d/usercron
** cronの使い方 まとめ [#hacea8b1]
cronのディレクトリ/etc/cron.d/の中に、自分の定期実行の設定ファイルを入れる。例えば、/etc/cron.d/usercronみたいな。
パスは絶対パスでかく。
# /etc/cron.d/usercron
SHELL=/bin/bash
usercron
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HOME=/home/fifi
# m h dom mon dow user command
# * * * * * root $HOME/anaconda3/bin/python $HOME/cron-script/crontest/crontest.py
# */1 * * * * root $SHELL $HOME/cron-script/crontest/crontest.sh
HOME=/home/fifi/Temp
SCRIPT=/home/fifi/Temp/task
CLOG=/home/fifi/Temp/task/log
# TEST SCRIPT
CRONSCRIPT = /home/fifi/cron-script/crontest
CLOG = /home/fifi/cron-script/crontest/log
*/1 * * * * root which python >> $CLOG/whichpython.log
*/1 * * * * root echo "echo log" >> $CLOG/cron-echo.log
*/1 * * * * root python $CRONSCRIPT/crontest.sh
# Copy ENV
*/1 * * * * root env $SCRIPT/cron_env
# Cron exe log
*/1 * * * * root date >> /home/fifi/Temp/task/log/bashdate.log
# Cron Python Script
*/1 * * * * root /bin/bash /home/fifi/Temp/task/crontest.sh
*/1 * * * * root python /home/fifi/Temp/task/crontest.py
** CRON -> Pythonスクリプト [#fae2692b]
パーミッションは適当でいい。オーナーはrootする必要もない。
--crontest.py
import datetime
LOG_DIR = "/home/fifi/Temp/task/log/crontestpy.log"
with open(LOG_DIR, "a") as fp:
fp.write(str(datetime.datetime.today()))
fp.write(" : crontest.py¥n")
** CRON -> シェルスクリプト -> Pythonスクリプト [#m967ca13]
パスは絶対パスでかく。
--crontest.sh
python /home/fifi/Temp/task/crontest_bash.py
--Permission (必要か分からない)
sudo chmod 700 crontest_bash.py
sudo chown root:root crontest_bash.py
--crontest_bash.py
import datetime
LOG_DIR = "/home/fifi/Temp/task/log/crontestpy.log"
with open(LOG_DIR, "a") as fp:
fp.write(str(datetime.datetime.today()))
fp.write(" : crontest_bash.py¥n")