STDOUT logs not working when using symlink for log file to /proc/1/fd/1 on Kubernetes

1/21/2020

I have a cronjob that runs every minute which will redirect output to a log file /var/log/cronjob/cron.log. Since this is running in Kubernetes, I want to redirect the log to STDOUT.

The approach I took was to use a symlink using RUN ln -sf /proc/1/fd/1 /var/log/cronjob/cron.log:

# ls -la /var/log/cronjob/cron.log
lrwxrwxrwx 1 root root 12 Jan 21 19:23 /var/log/cronjob/cron.log -> /proc/1/fd/1

When I run kubectl logs it has no output.

If I (within the container), delete the symlink and create as a normal file, my output as expected appears in the /var/log/cronjob/cron.log file.

# tail -f /var/log/cronjob/cron.log
Running scheduled command: '/usr/bin/php7.3' 'artisan' sync:health_check > '/dev/null' 2>&1
Running scheduled command: ('/usr/bin/php7.3' 'artisan' compute:user_preferences > '/dev/null' 2>&1 ; '/usr/bin/php7.3' 'artisan' schedule:finish "framework/schedule-9019c9dc22ad7439efd038277fe8f370f56958e7") > '/dev/null' 2>&1 &

How can I get the my log via symlink write to STDOUT?

I have tried other things such as:

  • Use /dev/stdout for the symlink
  • Tail the /var/log/cronjob/cron.log file within the entrypoint

Edit: More information about files/scripts:

crontab:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /usr/local/bin/schedule-run.sh
# An empty line is required at the end of this file for a valid cron file

/usr/local/bin/schedule-run.sh:

#!/bin/bash

# Source container environment variables
source /tmp/export

# Run Laravel scheduler
php /var/www/api/artisan schedule:run >> /var/log/cronjob/cron.log 2>&1

Edit #2:

Currently my CMD looks like this which spawns multiple child processes:

CMD export >> /tmp/export && crontab /etc/cron.d/realty-cron && cron && tail -f /var/log/cronjob/cron.log
root@workspace-dev-condos-ca-765dc6686-h8vdl:/var/www/api# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 21:55 ?        00:00:00 /bin/sh -c export >> /tmp/export && crontab /etc/cron.d/realty-cron && cron && tail -f /var/log/cronjob/cron.log
root         8     1  0 21:55 ?        00:00:00 cron
root         9     1  0 21:55 ?        00:00:00 tail -f /var/log/cronjob/cron.log
root       170     1  0 21:59 ?        00:00:00 ssh-agent -s
root       233     0  0 22:00 pts/0    00:00:00 bash
root       249     1  0 22:00 ?        00:00:00 ssh-agent -s
root      1277   233  0 22:26 pts/0    00:00:00 ps -ef

I'm not sure if that is relevant but through trial and error testing, I noticed that sometimes echo "test1" >> /proc/1/fd/1 or echo "test2" >> /proc/1/fd/2 will output to stdout (kubectl logs) but not both at the same time. I feel like the child processes are related but don't know why.

-- leeman24
cron
docker
kubernetes
stdout

0 Answers