title: Start python script with cron and output print to a file
body: <p>I'm starting a python script using cron, like this:</p>
  
  <pre><code>#Python script called every 3 minutes in case script crashes
  */3 * * * * /home/ubuntu/python_script.sh &amp;
  </code></pre>
  
  <p>And my script has several prints for debugging. I want to print those messages into a file, for example /home/ubuntu/Logs.txt</p>
  
  <p>My script also check if the python call is already running so it won't start it again:</p>
  
  <pre><code>#!/bin/bash
  lines=$(ps -ef | grep python_script.py | wc -l)
  
  if [ $lines -ne 2 ]; then
      python python_script.py &amp;
  fi
  </code></pre>
  
  <p>How do I print my messages to this file? I'm using python 2.7</p>
  
  <hr>
  
  <p>Update: Edited my code with the following:</p>
  
  <p>1) Added this as the first line of my python script:</p>
  
  <pre><code>from __future__ import print_function
  </code></pre>
  
  <p>2) Opened the log file after the last import, and right after that added a test print:</p>
  
  <pre><code>log = open("/home/ubuntu/Logs.txt", "w+")
  print("Testing...", file = log)
  </code></pre>
  
  <p>If I simple run it from the terminal, it prints to the file fine. But if I schedule it with cron it doesn't write to the file.</p>
  
  <hr>
  
  <p>Update 2: Ended up stumbling on a solution with "logger" command. On my shell script I had to add "2>&amp;1" (in the shell script, not crontab as described by ElmoVanKielmo) and " | logger &amp;". Final solution looks like this:</p>
  
  <pre><code>#!/bin/bash
  lines=$(ps -ef | grep python_script.py | wc -l)
  
  if [ $lines -ne 2 ]; then
      python python_script.py 2&gt;&amp;1 | logger &amp;
  fi
  </code></pre>
  
  <p>and it outputs any prints to /var/log/syslog file, which is good enough for me.</p>