简体   繁体   English

Crontab:使用Python并发送电子邮件附件

[英]Crontab: Using Python and sending email attachment

I currently have a python file that is executed by the below crontab. 我目前有一个由以下crontab执行的python文件。 It is currently working where it will run the Python file and it will email the output in the body of the email and also have an attachment with the same data. 目前,它正在运行Python文件的地方工作,它将通过电子邮件正文将输出通过电子邮件发送给您,并且还具有包含相同数据的附件。 If I am trying to only have the attachment ( and not show the output of the data in the email), how can I modify my cronjob to do this? 如果我只想拥有附件(不显示电子邮件中的数据输出),我该如何修改cronjob来做到这一点?

Python file: osversion_weekly.py Python档案:osversion_weekly.py

#! /usr/bin/python

import commands, os, string
import sys
import fileinput


output = os.system('uptime')
output = os.system('df')
output = os.system('top')
output = os.system('ps')

Crontab Job: Crontab职位:

04 19 17 10 2 /root/python/osversion_weekly.py | tee /root/python/osversion`date +\%Y-\%m-\%d-\%H:\%M`-cron.csv | mailx -s "Daily Report" -a "/root/python/osversion`date +\%Y-\%m-\%d-\%H:\%M`-cron.csv" myemail@apple.com

I have a different version of mailx than you so I can't test this, but I suspect you'll get what you want if you replace the command with the following: 我的mailx版本与您使用的版本不同,因此无法测试,但是我怀疑如果将命令替换为以下内容,将会得到所需的内容:

/root/python/osversion_weekly.py > /root/python/osversion`date +\%Y-\%m-\%d-\%H:\%M`-cron.csv; echo "" | mailx -s "Daily Report" -a "/root/python/osversion`date +\%Y-\%m-\%d-\%H:\%M`-cron.csv" myemail@apple.com

This writes the contents of the script to a file and then (hopefully) attaches it. 这会将脚本的内容写入文件,然后(希望)将其附加。

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

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