简体   繁体   English

使用从cron执行的python脚本写入文本文件

[英]Write to a textfile with python script that is executed from cron

Every time I run the script from cron, the file to which i write my stdout is empty, the files gets modified but nothing is written. 每次我从cron运行脚本时,我向其写入stdout的文件都为空,文件被修改,但未写入任何内容。 While running the script from terminal works. 从终端运行脚本时。

Example of what i'm trying to do: 我正在尝试做的例子:

run.py 运行

#!/bin/python2
import os
os.chdir('/home/user')
import getsmart
import sendemail

getsmart.py getsmart.py

#!/bin/python2

from subprocess import call
import os

os.chdir('/home/user')

f = open("result", "w")
call(["sudo","smartctl", "-a", "/dev/sda"], stdout=f)
call(["sudo","smartctl", "-a", "/dev/sdb"], stdout=f)

sendmail.py sendmail.py

#!/bin/python2

# Import smtplib for the actual sending function
import smtplib
import os

# Import the email modules we'll need
from email.mime.text import MIMEText

os.chdir('/home/user')

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
fp = open('result', 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = ''
msg['From'] = ''
msg['To'] = ''

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('')
s.sendmail('', '', msg.as_string())
s.quit()

cron 克朗

* * * * * /home/user/run.py

The script has execute permission. 该脚本具有执行权限。

-rwxr-xr-x. 1 user user 84 Jul  5 12:20 /home/user/run.py

As we can see next in the log the script is being executed: 正如我们接下来在日志中看到的,脚本正在执行:

Jul 5 13:11:01 localhost CROND[10945]: (root) CMD (/home/giumbai/run.py)


As of my understanding the problem is something regarding the way the executed commands output is written to stdout or how is read from stdout withing the script. 据我了解,问题出在执行命令输出写入stdout或如何通过脚本从stdout读取的方式。 The python documents doesn't helps me, neither searching the www. python文档对我没有帮助,也没有搜索www。

It is quite possible that your issue is due to the fact that you have opened the file and then written to it....but left it open. 您的问题很有可能是由于您已打开文件然后将其写入文件...。 You then attempt to open the file a second time but due to caching the data written to it is not yet available. 然后,您尝试再次打开文件,但是由于缓存,写入该文件的数据尚不可用。 If you change the first module to do the following instead: 如果将第一个模块更改为执行以下操作:

with open("result", "w") as f:
    call(["sudo","smartctl", "-a", "/dev/sda"], stdout=f)
    call(["sudo","smartctl", "-a", "/dev/sdb"], stdout=f)

This will open the file, write to it and then close it. 这将打开文件,写入文件,然后关闭它。 That should help! 那应该有帮助!

Another possibility is the fact that cron runs under a different environment than the normal shell. 另一种可能性是cron在与普通shell不同的环境中运行。 You may wish to include the full path for the smartctl command. 您可能希望包括smartctl命令的完整路径。 On my machine that is /usr/sbin/smartctl. 在我的机器上是/ usr / sbin / smartctl。

In addition, it might be better to install the script under root's crontab instead of the local user and use sudo -- there may be interactions with the environment that don't work under cron there as well. 另外,最好将脚本安装在root用户的crontab下而不是本地用户下,并使用sudo -与环境的交互也可能不在cron下工作。

And one other option to consider: make sure that stderr goes somewhere as well so you can detect errors -- that would probably help you figure out what the trouble is when running with cron. 还有一个可供考虑的选项:确保stderr也能到达某个地方,以便您可以检测到错误-这可能会帮助您弄清楚使用cron运行时的麻烦所在。 Normally this goes the system log or the local user's mail but depending on your configuration that may not be that helpful! 通常,这会进入系统日志或本地用户的邮件,但取决于您的配置,可能没有帮助!

As a side note, it is good practice not to use the import of a module to do a script's work. 附带说明一下,优良作法是不要使用模块的导入来完成脚本的工作。 A module should have classes and functions defined in it which a script (your run.py) then calls. 模块中应定义有类和函数,脚本(您的run.py)随后可以调用这些类和函数。 Or just put the code in run.py directly! 或者直接将代码放入run.py中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM