0

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])
asked Jul 23, 2015 at 17:11
13
  • 1
    Is that cron log file working? You aren't sending any output to mailx where is it supposed to be getting the content of your email from? Commented Jul 23, 2015 at 17:18
  • 1
    Does the correct /var/log/cronXXXXX-cron.log file get created at least? Even if it then empty? Commented Jul 23, 2015 at 17:50
  • 1
    Does using $(...) instead of backticks help any? Can you run env -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? Commented Jul 23, 2015 at 18:25
  • 1
    I doubt that would help. That env command 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 to mailx. Commented Jul 24, 2015 at 1:27
  • 1
    You seemed to want the output in an email in the first place. If that's the case then yes, drop the redirection to the file entirely and instead just pipe it to mailx (probably with standard error too 2>&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`). Commented Jul 24, 2015 at 1:34

2 Answers 2

1

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]
answered Jul 23, 2015 at 17:17
Sign up to request clarification or add additional context in comments.

3 Comments

I do get "test" in the subject of the email, should I also be getting "hello" as well?
No "test" is just the subject. "hello" is the whole message body. It’s just like 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.
Sorry, I'm not really clear on what I need to do differently. Right now in crontab I've got the following: 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]
0

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]
answered Jul 26, 2015 at 3:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.