0

I have configured crontab to have the following entry:

*/2 * * * * source /home/ubuntu/cronenv/python2.7/bin/activate && python /home/ubuntu/trial.py  >> /var/log/mycron/trial.log 2>&1 && deactivate

By tailing the /var/log/syslog file, I can verify that the cron runs every two minutes. This is the entry in /var/log/syslog every two minutes

Nov  8 10:52:01 ip-172-31-0-41 CRON[2023]: (ubuntu) CMD (source /home/ubuntu/cronenv/python2.7/bin/activate && python /home/ubuntu/trial.py  >> /var/log/mycron/trial.log 2>&1 && deactivate)

By running the command in terminal, it runs as intended and creates trial.log file in /var/log/mycron/

The folder has all the required permissions as shown below:

drwxrwxrwx  2 root   root     4096 Nov  8 11:02 mycron

Please help me in figuring out the issue.

7
  • You are missing the PATH in your cron. You either set the PATH env var correctly or use the absolute path to python Nov 8 '17 at 11:04
  • Shouldn't activating the virtualenv take care of python path?
    – G.One
    Nov 8 '17 at 11:08
  • 1
    Probably source is failing, so && short circuits. Try redirecting stderr/out to a file from the first source. You should see the error. (aside, this is one of the nice things about using systemd timers instead of cron, debugging is way easier)
    – Chris Down
    Nov 8 '17 at 11:10
  • @ChrisDown let me try that and check the output
    – G.One
    Nov 8 '17 at 11:12
  • 1
    If you're using Ubuntu, then cron's default shell /bin/sh is dash - which doesn't support the source synonym for . Nov 8 '17 at 11:49
2

It seems likely that source is failing, so && short circuits. Try redirecting stderr/out to a file from the first source and you should see the error.

If trial.py ran at all, you should at least see trial.log created by the shell. The fact that it's not created suggests that this code was never run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.