<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Stdout on blog.iankulin.com</title><link>https://blog.iankulin.com/tags/stdout/</link><description>Recent content in Stdout on blog.iankulin.com</description><generator>Hugo</generator><language>en-AU</language><lastBuildDate>Mon, 08 Jul 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.iankulin.com/tags/stdout/index.xml" rel="self" type="application/rss+xml"/><item><title>Outputting to the console, in Docker, from a cron job</title><link>https://blog.iankulin.com/outputting-to-the-console-in-docker-from-a-cron-job/</link><pubDate>Mon, 08 Jul 2024 00:00:00 +0000</pubDate><guid>https://blog.iankulin.com/outputting-to-the-console-in-docker-from-a-cron-job/</guid><description>&lt;p&gt;&lt;img src="https://blog.iankulin.com/images/screen-shot-2024-07-02-at-3.48.02-pm.jpg" alt=""&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re googling this exact title, you&amp;rsquo;re probably bumping your head against the same things I was today. I was debugging a completely different project, and needed to print to the console, from a &lt;code&gt;cron&lt;/code&gt; job, in a Docker container. Turns out this isn&amp;rsquo;t as straightforward as I thought.&lt;/p&gt;
&lt;h3 id="foreground-cron"&gt;Foreground cron&lt;/h3&gt;
&lt;p&gt;Before you even get to the problem space, here&amp;rsquo;s a tip. If you want to have a cron job running in a container, start &lt;code&gt;cron&lt;/code&gt; in the foreground. If you do not, Docker realises nothing is going on, and exits. If you want to keep the container active so your &lt;code&gt;cron&lt;/code&gt; jobs get a chance to execute, then start it in the foreground.&lt;/p&gt;
&lt;h3 id="stdout"&gt;stdout&lt;/h3&gt;
&lt;p&gt;I always think about &lt;code&gt;stdout&lt;/code&gt; as being the console, but I guess at one time it was probably a teletype printer, and for &lt;code&gt;cron&lt;/code&gt;, &lt;a href="https://askubuntu.com/questions/1454389/where-does-the-users-cron-output-go-to-by-default-on-ubuntu"&gt;apparently it&amp;rsquo;s an email to the user&lt;/a&gt;. So me directing the output of the command I&amp;rsquo;m running in crontab to stdout is not helpful. It turns out you need something like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;* * * * * /script.sh &amp;gt; /proc/1/fd/1 2&amp;gt;&amp;amp;1
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="https://github.com/moby/moby/issues/19616#issuecomment-174492543"&gt;There&amp;rsquo;s some good Linuxy reason for this&lt;/a&gt; - &lt;code&gt;/proc/1/fd/1&lt;/code&gt; is the standard output of a particular process which happens to be the process for the entry point of the container or some such thing,&lt;/p&gt;
&lt;p&gt;If you are not familiar with &lt;code&gt;cron&lt;/code&gt;, there are going to be much better explanations than this, but it&amp;rsquo;s a mechanism for running jobs at various times. It uses a file, called the &lt;code&gt;crontab&lt;/code&gt; to define these. Each of the asterisks above are spots where we can define the minute, hour, day, etc that the job runs by entering a number. If they are all asterisks then we are saying &amp;lsquo;run this every minute&amp;rsquo;. Following that is just the command, which in this case is run &lt;code&gt;/script.sh&lt;/code&gt; and send the output and error output to this proc file which happens to be the console.&lt;/p&gt;
&lt;p&gt;Other gotchas when working with cron is which user it&amp;rsquo;s running as (so permission problems) and where it&amp;rsquo;s running from (don&amp;rsquo;t use relative file paths).&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/IanKulin/cron-docker-output"&gt;Example project on GutHub&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>