2

So I have a python script that pulls down git/svn/p4 repositories and does some writing to a database.

I'm simply trying to automate the running of this script and based from what I see in syslog it is being run, even the file I tried piping the output to is being created however the file is empty. Here is the cron job:

10 04 * * * user /usr/bin/python2.7 /home/user/script.py -f someFlag > ~/cronout.log 2&>1

Kinda stumped and don't know where to start. Thinking its maybe the requirement of passwords for the keychain and what not. Any ideas would be helpful!

ashrles
  • 21
  • Have you tried using a lockfile? See my answer here. Also, does the script produce the expected output if run in a terminal? – eyoung100 Oct 02 '14 at 15:29
  • Yes the script produces expected output when run from terminal. It prompts of password and then it pulls down all the repos one u authenticate it. I was able to make you answer work for me sadly. Thanks tho – ashrles Oct 07 '14 at 13:58
  • Did you mean wasn't?? Do you mind posting script.py? – eyoung100 Oct 07 '14 at 14:05
  • Yeah sorry, meant "wasn't". And can't post the script, don't want to get fired x) unhelpful... i know – ashrles Oct 07 '14 at 14:09
  • OK, Does the cron job run at 4:10am everyday? – eyoung100 Oct 07 '14 at 14:12
  • Yes it does. Whenever I'm testing I change the time accordingly and it always runs according to syslog, and the output file is created but not populated. – ashrles Oct 07 '14 at 17:36

2 Answers2

3

Use tee to grab the output

Tee Manual

tee reads from Standard INPUT/OUTPUT to write files. I have a Python script which supports one of my requirements, and simple ">" redirection fails. Tee is the way to grab output.

Your line should be:

10 04 * * * user /usr/bin/python2.7 /home/user/script.py -f someFlag | tee -a ~/cronout.log 2>&1

Try this: Wrap your python script in a #!/bin/bash script in the /usr/bin/ directory.

Then replace your line:

10 04 * * * user /usr/bin/myPythonWrapperInBash& > /path/to/log/out 2>&1

Explanation of Tee Invocation

  • [-a] appends the file.
  • Vanilla invocation (no flags) would overwrite the file, and you would lose your log.
  • Thanks, thats good to know! But it didn't work for me sadly :( once again cronout.log is created but nothing written to it. – ashrles Oct 07 '14 at 14:00
0

So it turns out the problem was with environment variables that the Python script needed, and it was so early on in the script that it broke the script before it even output anything.

Cron does not have a regular environment.

Furthermore ssh passwords were required for pulling git repos which i was able to solve by using Keychain.

Using the help of this blog post and some bash wrapper scripts I was able to get everything working and automated.

ashrles
  • 21