crontabでModuleNotFoundError: No module named ‘mysql’

hoge.py

import requests
import mysql.connector
import config
from datetime import datetime
// 省略

$ sudo vi /etc/crontab

0 0    * * *   root    /usr/bin/python3 /home/ubuntu/hoge/cmd/hoge.py > /var/log/myjob.log 2>&1

$ cat /var/log/myjob.log
Traceback (most recent call last):
File “/home/ubuntu/reddit/cmd/_cardano.py”, line 2, in
import mysql.connector
ModuleNotFoundError: No module named ‘mysql’

なぜだあああああああああああ
2時間ぐらい格闘して

hoge.py

#!PYTHONPATH=/home/ubuntu/.local/lib/python3.8/site-packages
import requests
import mysql.connector
import config
from datetime import datetime

$ sudo service cron restart

これで上手くいった…

なぜ上手くいったのかさっぱりわからない….
とりあえずOKだわ、上手く動いているか明日再度確認する

env command

The env command sets an environment variable and executes a program.

option
– 0 : Make the end of the displayed line not a new line but a byte with a value of 0.
– i : Run the program without envrionment variables.
– u : variable name if the specified variable name is in the list of environment variables, delete it.

[vagrant@localhost ~]$ env
HOSTNAME=localhost.localdomain
NVM_CD_FLAGS=
PYENV_ROOT=/home/vagrant/.pyenv
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.35.1 63549 22
SSH_TTY=/dev/pts/0
NVM_DIR=/home/vagrant/.nvm
USER=vagrant
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
PYENV_VIRTUALENV_INIT=1
MAIL=/var/spool/mail/vagrant
PATH=/home/vagrant/.nvm/versions/node/v10.12.0/bin:/home/vagrant/.pyenv/plugins/pyenv-virtualenv/shims:/home/vagrant/.pyenv/shims:/home/vagrant/.pyenv/bin:/home/vagrant/.rbenv/shims:/home/vagrant/.rbenv/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/vagrant/bin
PWD=/home/vagrant
LANG=ja_JP.UTF-8
RBENV_SHELL=bash
HISTCONTROL=ignoredups
PYENV_SHELL=bash
SHLVL=1
HOME=/home/vagrant
LOGNAME=vagrant
SSH_CONNECTION=192.168.35.1 63549 192.168.35.10 22
NVM_BIN=/home/vagrant/.nvm/versions/node/v10.12.0/bin
LESSOPEN=||/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/bin/env

Reflection after changing crontab file

How to reflect changes after editing the contents of crontab file directly. There is no need to restart cron after changing crontab.

On Linux, it checks the modification time of cron every minute and reloads if there is a change, so change of the file of cront if you do, your changes will take effect immediately without having restart cron.

/var/spool/cron

[vagrant@localhost ~]$ /etc/rc.d/init.d/crond status
crond (pid 2614) を実行中…

batch処理

バッチ処理一覧
|No.|業務ID|業務名|JOBID|JOB名|説明|実行サーバー|処理サイクル|起動時間|備考|