我对linux比较新,所以希望这很简单,虽然我无法通过搜索找到答案。 我正在使用cron(Debian Jessie在Raspberry pi上)每天凌晨1点运行一个脚本,它会在不到一天的时间内休眠一段时间,然后运行另一个脚本。它工作正常,但我也尝试实现一个日志,以便我可以看到脚本运行的时间列表,并注意它是否没有按时启动。当我手动运行脚本时,日志写得很好,但是当我通过cron触发它时,不会创建任何日志。有人有建议吗?代码如下:
#!/usr/bin/python
from random import randint
import os
from time import sleep
import logging
import datetime
now = datetime.datetime.now()
time = now.hour*3600 + now.minute*60 + now.second # current time in seconds
logging.basicConfig(filename='script.log',level=logging.DEBUG)
sleep_time = randint(0,70000)
total_sec = sleep_time + time
hours = total_sec/3600
minutes = (total_sec%3600)/60
seconds = total_sec%60
if minutes < 10:
minutes = "0" + str(minutes)
if seconds < 10:
seconds = "0" + str(seconds)
logging.info(" Job started at " + str(now) + ". Will sleep for " + str(sleep_time) + " seconds. Script will begin at " + str(hours) + ":" + str(minutes) + ":" + str(seconds) + ".")
if time < 3500 or time > 3700:
logging.warning(" Script did not start at specified time. Script began at " + str(now))
sleep(sleep_time)
os.system("/path/to/program.py")