[英]Python3 script in crontab in ubuntu
eric@eric:~/project$ tree -d
.
|-- env
| |-- bin
| |-- include
| | `-- python3.4m -> /usr/include/python3.4m
| `-- lib
| `-- python3.4
| |-- collections -> /usr/lib/python3.4/collections
| |-- config-3.4dm-x86_64-linux-gnu -> /usr/lib/python3.4/config-3.4dm-x86_64-linux-gnu
| |-- config-3.4m-x86_64-linux-gnu -> /usr/lib/python3.4/config-3.4m-x86_64-linux-gnu
| |-- distutils
| | `-- __pycache__
| |-- encodings -> /usr/lib/python3.4/encodings
| |-- importlib -> /usr/lib/python3.4/importlib
| |-- lib-dynload -> /usr/lib/python3.4/lib-dynload
| |-- plat-x86_64-linux-gnu -> /usr/lib/python3.4/plat-x86_64-linux-gnu
| |-- __pycache__
| `-- site-packages
| ... # removed thirdparty libraries just to make it more readable
| |-- pip-1.5.6.dist-info
| |-- __pycache__
| |-- setuptools
| | |-- command
| | | `-- __pycache__
| | |-- __pycache__
| | `-- tests
| | `-- __pycache__
| `-- setuptools-3.6.dist-info
`-- myscript.py
so in home directory there is a folder named project. 因此在主目录中有一个名为project的文件夹。 in project folder I have a virtualenv folder and a simple script(let's say hello world in python3)
在项目文件夹中,我有一个virtualenv文件夹和一个简单的脚本(比方说python3中的hello world)
which python3
gives /usr/bin/python3
which python3
赋予/usr/bin/python3
which python3
Now I'm trying to run this script using crontab -e
(ubuntu 14.04). 现在,我正在尝试使用
crontab -e
(ubuntu 14.04)运行此脚本。 I can run this script manually by activating virtualenv ( source env/bin/activate
) but I've no idea how should I do this thing in crontab and what would be the shebang for this python3 virtualenv. 我可以通过激活virtualenv(
source env/bin/activate
)手动运行此脚本,但是我不知道该如何在crontab中执行此操作,以及此python3 virtualenv的shebang是什么。
PS: I'm able to run a python2 script in crontab by adding this shebang #!/usr/bin/python
at top of the file and then this line in crontab -e (run everyday at 2PM ) PS:通过在文件顶部添加此shebang
#!/usr/bin/python
,然后在crontab -e中此行(每天下午2点运行),我可以在crontab中运行python2脚本
00 14 * * * /usr/bin/python /home/eric/project/myscript.py >> /var/log/cronjob.log
PPS: I dont want to write setup.py and use setuptools just a natural way (as I did above for python2) PPS:我不想写setup.py并使用setuptools只是一种自然的方式(就像我上面对python2所做的那样)
If I understood right, you want to run your script with python3
from virtualenv
. 如果我理解正确,那么您想使用
virtualenv
python3
运行脚本。
If so, you should use this path in your crontab -e
: /home/eric/project/env/bin/python
(figure out the exact path, if this one doesn't work). 如果是这样,则应在
crontab -e
使用此路径: /home/eric/project/env/bin/python
(找出正确的路径,如果该路径不起作用)。
I think, shebang has no nothing to do here, once you specify python binary explicitly. 我认为,一旦您明确指定python二进制文件,shebang在这里就无事可做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.