title: Python Script not running in cron
body: <p>I am trying to run a Python script from cron. I am using crontab to run the command as a user instead of root. My Python script has the shebang at the top <code>#! /usr/bin/env python</code> and I did <code>chmod +x</code> it to make the script executable. </p>
  
  <p>The script does work when running it from a shell but not when using crontab. I did check the <code>/var/log/cron</code> file and saw that the script runs but that absolutely nothing from <code>stdout</code> or <code>stderr</code> prints anywhere. </p>
  
  <p>Finally, I made a small script that prints the date and a string and called that every minute that worked but this other script does not. I am not sure why I am getting these variable results... </p>
  
  <p>Here is my entry in <code>crontab</code> </p>
  
  <pre><code>SHELL=/bin/bash
  
  
  
  #min    #hour   day-of-month    month   day-of-week     command
  #-------------------------------------------------------------------------
  */5     *       *                *        *             /path/to/script/script.py
  </code></pre>
  
  <p>Here is the source code of my script that will not run from crontab but will run from the shell it is in when called like so <code>./script.py</code>. The script is executable after I use the <code>chmod +x</code> command on it, by the way...:</p>
  
  <pre><code>#! /usr/bin/env python
  
  #create a file and write to it
  import time
  
  def create_and_write():
      with open("py-write-out.out", "a+") as w:
          w.write("writing to file. hello from python. :D\n")
          w.write("the time is: " + str(time.asctime()) + "\n")
          w.write("--------------------------------\n")
  
  
  def main():
      create_and_write()
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p><strong>EDIT</strong>
  I figured out what was going wrong. I needed to put the absolute file paths in the script otherwise it would write to some directory I had not planned for. </p>