<p>I have a super simple container with cron scheduled:</p>

<pre><code>* * * * * root /bin/bash /alive.sh
</code></pre>

<p>alive.sh:</p>

<pre><code>#!/bin/bash

/bin/echo "I'm alive"
/bin/echo $(/bin/date) &gt;&gt; /tmp/alive.log
</code></pre>

<p>I build the docker image locally and run it:</p>

<pre><code>docker build -t orian/crondemo:v0 .
docker run --rm -it --name crondemo orian/crondemo:v0
</code></pre>

<p>And after a minute or so I can check that a new file is being created:</p>

<pre><code>docker exec crondemo ls /tmp
</code></pre>

<p>The I tag and push the image to Google Container Registry:</p>

<pre><code>TAG=eu.gcr.io/&lt;PROJECT_ID&gt;/crondemo:v0
docker tag orian/crondemo:v0 $TAG
kubectl docker -- push $TAG
</code></pre>

<p>Starting a pod manually:</p>

<pre><code>kubectl run crondemo --image=$TAG --replicas=1 --restart=Never
</code></pre>

<p>And verifying that it works:</p>

<pre><code>kubectl exec crondemo ls /tmp
</code></pre>

<p>And here is a problem, a <code>/tmp/alive.log</code> file is not being written to. Where is the problem?</p>

<p>I've prepared a repo with sample: <a href="https://github.com/orian/k8s-cron-demo" rel="nofollow noreferrer">https://github.com/orian/k8s-cron-demo</a></p>

<h3>Notice</h3>

<ul>
<li>I've also tested overwriting <code>/var/spool/cron/crontabs/root</code> but it didn't solve the problem.</li>
<li>I'm using docker image: <code>openjdk:8-jre</code>. Before switching I used <code>alpine</code> and <code>crond</code>. It's seemed to work then.</li>
</ul>

<h3>Edit2 - found (this is crazy):</h3>

<ul>
<li><a href="https://forums.docker.com/t/running-cronjob-in-debian-jessie-container/17527/2" rel="nofollow noreferrer">https://forums.docker.com/t/running-cronjob-in-debian-jessie-container/17527/2</a></li>
<li><a href="https://stackoverflow.com/questions/21926465/issues-running-cron-in-docker-on-different-hosts">Issues running cron in Docker on different hosts</a></li>
<li><a href="https://stackoverflow.com/questions/21391142/why-is-it-needed-to-set-pam-loginuid-to-its-optional-value-with-docker">Why is it needed to set `pam_loginuid` to its `optional` value with docker?</a></li>
</ul>

<p>I've followed a <a href="https://stackoverflow.com/a/21928878/436754">https://stackoverflow.com/a/21928878/436754</a> on enabling logs.</p>

<p>Running: <code>/var/log/syslog</code> </p>

<pre><code>May  4 12:33:05 crondemo rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="14" x-info="http://www.rsyslog.com"] start
May  4 12:33:05 crondemo rsyslogd: imklog: cannot open kernel log(/proc/kmsg): Operation not permitted.
May  4 12:33:05 crondemo rsyslogd-2145: activation of module imklog failed [try http://www.rsyslog.com/e/2145 ]
May  4 12:33:08 crondemo cron[38]: (CRON) INFO (pidfile fd = 3)
May  4 12:33:08 crondemo cron[39]: (CRON) STARTUP (fork ok)
May  4 12:33:08 crondemo cron[39]: (*system*) NUMBER OF HARD LINKS &gt; 1 (/etc/crontab)
May  4 12:33:08 crondemo cron[39]: (*system*crondemo) NUMBER OF HARD LINKS &gt; 1 (/etc/cron.d/crondemo)
May  4 12:33:08 crondemo cron[39]: (CRON) INFO (Running @reboot jobs)
May  4 12:34:01 crondemo cron[39]: (*system*) NUMBER OF HARD LINKS &gt; 1 (/etc/crontab)
May  4 12:34:01 crondemo cron[39]: (*system*crondemo) NUMBER OF HARD LINKS &gt; 1 (/etc/cron.d/crondemo)
</code></pre>

<p>This made me Google for <code>cron "NUMBER OF HARD LINKS &gt; 1"</code> and I've found: <a href="https://github.com/phusion/baseimage-docker/issues/198" rel="nofollow noreferrer">https://github.com/phusion/baseimage-docker/issues/198</a></p>

<p>The workaround is to modify a <code>Dockerfile</code> to overwrite the cron file on start instead being mounted by Docker.</p>

<ul>
<li>Dockerfile <code>COPY cronfile /cronfile</code></li>
<li><code>docker-entrypoint.sh</code>: <code>cp /cronfile /etc/cron.d/crondemo</code></li>
</ul>

<p>A branch with workaround: <a href="https://github.com/orian/k8s-cron-demo/tree/with-rsyslog" rel="nofollow noreferrer">https://github.com/orian/k8s-cron-demo/tree/with-rsyslog</a></p>
