LoginSignup
3
6

More than 3 years have passed since last update.

ubuntu環境にpythonのcronを設定手順

Last updated at Posted at 2019-01-16

ubuntu環境ログイン。
cronが設定されているか確認。

$ crontab -l

cronの設定。

$ crontab -e

エディターがnanoなのでvimに変更する。

$ update-alternatives --config editor

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
# Edit this file to introduce tasks to be run by cron.
------------------------------------------------------------
* 0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode

#  Default rules for rsyslog.
Press <enter> to keep the current choice[*], or type selection number:

vimに変更したいので、「3」を選択するとvimになります。

それでも変わらない場合は、

$ select-editor

Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

Choose 1-4 [2]:

こちらも「3」を選択。

ついでにpythonの場所を確認

$ which python
/home/ubuntu/.pyenv/shims/python

気を取り直して、cronを設定。

$ crontab -e
# cron実行
05 13 * * * $HOME/.pyenv/shims/python /home/ubuntu/xxxx.py

# cron実行 + 標準ログ出力 + エラーログ出力
05 13 * * * $HOME/.pyenv/shims/python /home/ubuntu/xxxx.py > /var/log/cron.log 2>&1

cronの細かい設定はこちらの記事が参考になります。

クーロン(cron)をさわってみるお

ubuntu初期設定のままだとcronのログが出ないみたいなので、ログが表示されるように変更します。

$ vim /etc/rsyslog.d/50-default.conf

こちらのファイルの10行目くらいに以下の記述がコメントアウトされているので外す。

cron.*                          /var/log/cron.log

設定は反映されなかったら、cronを再起動する。

$ /etc/init.d/cron restart
[ ok ] Restarting cron (via systemctl): cron.service.
3
6
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
3
6