Relatively new to running cron jobs in Centos6, I can't seem to get this Python script to execute properly. I would like this script to execute and then email me the output. I have been receiving emails, but they're empty.
So far, in Crontab I've tried entering:
*/10 * * * * cd /home/local/MYCOMPANY/purrone/MyPythonScripts_Dev1 && /usr/bin/python ParserScript_CampusInsiders.py > /var/log/cron`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log 2>&1 ; mailx -s "Feedparser Output" [email protected]
and
*/10 * * * * /home/local/MYCOMPANY/purrone/MyPythonScripts_Dev1/ParserScript_CampusInsiders.py > /var/log/cron`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log 2>&1 ; mailx -s "Feedparser Output" [email protected]
I have run chmod +x on the python script to make the script executable and the Python script has #!/usr/bin/env python at the header. What am I doing wrong here?
The other problem might be that I shouldn't be using the log file? All I see at /var/log/cron when I open with cat cron is entires like this, for example (no actual output from the script):
Jul 23 13:20:01 ent-mocdvsmg01 CROND[24681]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jul 23 13:20:01 ent-mocdvsmg01 CROND[24684]: (MYJOB\purrone) CMD (/home/local/MYCOMPANY/purrone/MyPythonScripts_Dev1/ParserScript_CampusInsiders.py > /var/log/cron`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log 2>&1 ; mailx -s "Feedparser Output" [email protected])
2 Answers 2
There is nothing going into your mailx input; it expects the message on stdin. Try running it outside of crontab as a test until it sends a valid email. You could test with:
% echo hello |mailx -s test [email protected]
Note that cron can email you the output of its run. You just need to add a line to the top of crontab like:
MAILTO = [email protected]
3 Comments
cat full-message-body.txt | mailx ... or mailx ... < full-message-body. So instead of cat you want your python script sending its stdout into mailx.MAILTO = [email protected] */10 * * * * /home/local/mycompany/chris/SilverChalice_CampusInsiders/SilverChalice_CampusInsiders.py > date +\%Y-\%m-\%d-\%H:\%M:\%S-cron.log 2>&1; mailx -s "SilverChalice Feedparser" [email protected] Solution was to omit the redirect > and instead edit the Crontab thusly:
*/15 * * * * /home/local/COMPANY/malvin/SilverChalice_CampusInsiders/SilverChalice_Parser.py | tee /home/local/COMPANY/malvin/SilverChalice_CampusInsiders`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log | mailx -s "SilverChalice CampusInsiders" [email protected]
mailxwhere is it supposed to be getting the content of your email from?/var/log/cronXXXXX-cron.logfile get created at least? Even if it then empty?$(...)instead of backticks help any? Can you runenv -i sh -xc '/home/local/MYCOMPANY/purrone/MyPythonScripts_Dev1/ParserScript_CampusInsiders.py > /var/log/cron`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log 2>&1'correctly? Does it create the log file?envcommand pattern is one of the debugging steps for non-working cron commands (in the crontab info wiki) though. So your command itself works in the cron environment but something else is going on that is keeping it from working correctly in cron itself. Wait... is that per-user crontab trying to write to/var/log? That's not going to work. Try some other directory and/or stop that and just pipe tomailx.mailx(probably with standard error too2>&1). Otherwise, if you do want a log file, and that is a per-user crontab, then use a directory that your user can create files in (i.e. not/var/log`).