centos6.8+crontab无法定时执行python3.5脚本(可以定时执行python2.6)

 叶毒手_938 发布于 2022-10-31 10:25

我上一篇问题也是这个,但当时没有发现在同一台机子下的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
2 个回答
  • 还没找到解决方法,现在只能把中文字符去掉了

    2022-10-31 22:17 回答
  • 编码问题?试试在字符串前面加个u看看。

    2022-10-31 22:17 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有