Directing output from cron (calls) bash (calls) python3
<p>I have a simple python script, <code>test.py</code>, which prints the date and time and then raises an error.  </p>

<p>I have a bash function defined in <code>.bash_profile</code> and named <code>test()</code>, which calls the script with </p>

<pre><code>$ python3 ~/test.py
</code></pre>

<p>Finally, I have a cron line set to call the <code>test()</code> bash function once a minute for testing with </p>

<pre><code>test &gt;&gt; ~/$(date +\%Y-\%m-\%d_\%H:\%M:\%S).log 2&gt;&amp;1
</code></pre>

<p>When I run the python script or the bash function, I correctly get both the print and the error to the terminal.  When cron calls the python script directory, it logs correctly.  But when cron calls the bash function, nothing is written to the log file.  </p>

<p><strong>Question</strong></p>

<p>How do I correctly direct the output of the python script to the log file when cron calls the bash function?</p>

<p>First of all <code>test</code> is a bad name for a function as almost all shells have <code>test</code> builtin and also there is external <code>test</code> command available in almost all systems.</p>

<p>Now, when you run something in <code>cron</code>, unlike starting of a login and/or interactive shell session no session startup script is read hence the function defined in <code>~/.bash_profile</code> (<code>source</code>-d while starting login session) is not being available.</p>

<p>Note that, many systems do not use <code>bash</code> to run <code>cron</code> scripts, for example Ubuntu uses <code>dash</code>. Anyway, the <code>test</code> you are executing in <code>cron</code> is presumably that shell's builtin <code>test</code> command which will return exit code 1 without any argument.</p>

python bash cron