centos6.8+crontab无法定时执行python3.5脚本(可以定时执行python2.6)
PHP中文网
PHP中文网 2017-04-18 10:03:07
0
2
518

我上一篇问题也是这个,但当时没有发现在同一台机子下的python2.6可以定时执行,现在我从新理一下我的问题:

centos版本

Linux centos6 2.6.32-358.18.1.el6.x86_64 #1 SMP Wed Aug 28 17:19:38 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

python2.6命令

[root@centos6 home]# python2
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

python3.5命令

[root@centos6 home]# python
Python 3.5.0 (default, Nov  5 2016, 11:04:57) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

/home目录下放了两个文件,mkf_2代表使用2.6版本,mkf_3代表使用3.5版本,两个脚本的作用都是在home目录下生成时间戳加中文命名的文件

脚本1:mkf_2.py

#!/usr/bin/env python2
# coding:utf-8
import time

file_name = str(time.time())+'文章'
f = open('/home/'+file_name,'w')
f.close()

脚本2:mkf_3.py

#!/usr/bin/env python
import time

file_name = str(time.time())+'文章'
f = open('/home/'+file_name,'w')
f.close()

脚本运行效果,在home目录下正常生成文件

把文件删除,然后在crontab -e 中添加如下命令,定时每分钟运行脚本1,把脚本2注释掉

过了几分钟,查看home目录下的文件,可以看到脚本成功执行了,再把文件删掉

然后把crontab修改为执行脚本2,注释第一行

过了好久,没有发现生成文件

crontab日志和报错都没有发现异常

换了一台服务器也是这种情况

请问这是怎么回事呢?


问题更新:如果把脚本2做一下改动,把中文字符去掉,定时器就可以执行脚本,可为什么有中文定时执行不了

[root@centos6 home]# cat mkf_3.py 
#!/usr/bin/env python
import time

file_name = str(time.time())
f = open('/home/'+file_name,'w')
f.close()
[root@centos6 home]# ls 14*
1480590721.7563603  1480590781.7921832
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(2)
迷茫

编码问题?试试在字符串前面加个u看看。

迷茫

还没找到解决方法,现在只能把中文字符去掉了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!