seacnboy 发表于 2014-05-09 16:06

crontab下脚本运行问题

脚本名为hc.py
root@gmcdmas1o> ls -l
total 38
-rwxr-xr-x   1 hainms      15877 May9 15:46 hc.py
crontab名为:
47 15 * * */home/hai/hc/hc.py
直接在shell命令行执行hc.py 能够正常运行,
但是用crontab也能执行,但只是执行到
record_file.write('###############check cpu status########################\n')
report_file.write('###############check cpu status########################\n')
就再也不继续执行了。。
换过47 15 * * *python /home/hai/hc/hc.py也不行
求解答,谢谢!!
下面是程序示例:

#!/usr/bin/env python
import commands,time,os,glob

date_time=time.strftime('%Y%m%d%H%M%S',time.localtime())
host_name=commands.getoutput('hostname')
record_file1='%s%sHc_record.log' %(host_name,date_time)
report_file1='%s%sHc_report.log' %(host_name,date_time)
record_file=open(r'/home/zhaohai/hc/log/'+record_file1,'w+')
report_file=open(r'/home/zhaohai/hc/log/'+report_file1,'w+')

record_file.write('###############check cpu status########################\n')
report_file.write('###############check cpu status########################\n')
top_info1=commands.getoutput('top -n 20')

timespace 发表于 2014-05-09 16:16

“shell可以,cron不行”,都是环境变量问题。仔细确认要依赖的PATH等环境变量,该设置的设置,该用绝对路径就用,比如cron直接调"hostname"或"top"通常无法成功。另外这个问题更适合shell版,和Python没关系。

friendmine 发表于 2014-05-09 17:03

1.看log.一般都会在/var/log下有一个log吧.
2.你这个可能是代码在输入文件时,没有写权限.看一下执行用户是那一个吧.

q1208c 发表于 2014-05-10 10:22

:-L 写了这么复杂个python代码, 结果就是个 top -n 20 的输出. 楼主NB呀.

Perl_Er 发表于 2014-05-10 21:41

回复 1# seacnboy


    -b

初识orcl 发表于 2014-05-27 13:18

看/var/log里面controb的日志吧
页: [1]
查看完整版本: crontab下脚本运行问题