Big J Big J - 7 months ago 55
Linux Question

Crontab: Using Python and sending email attachment

I currently have a python file that is executed by the below crontab. 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. 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?

Python file:

#! /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:

04 19 17 10 2 /root/python/ | 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"


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:

/root/python/ > /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"

This writes the contents of the script to a file and then (hopefully) attaches it.