Docker Community Forums

Share and learn in the Docker community.

Cron as Docker Container, everything is not found

I have a problem with cron in a docker container. Everything I want to start is not being found. I have searched much about this but I only found rather old threads so I decided to add a new one.

I want to have a docker container with running crond on my QNAP TS453pro.

My dockerfile is

FROM alpine
COPY ENTRYPOINT.sh /ENTRYPOINT.sh
ENTRYPOINT ["/ENTRYPOINT.sh"]

It is started from docker-comopse

    cron:
    build: './cron/'
    container_name: qnap_cron
    hostname: qnap_cron
    volumes:
        - /share/:/share/
        - /share/config/perm/cron/CRONJOBS.sh:/CRONJOBS.sh
    environment:
        - CRON_LOGLEVEL=7
    networks:
        - backend
    restart: always

ENTRYPOINT.sh

#!/bin/sh
set -e
echo "$1" | crontab /CRONJOBS.sh && crond -f -L /dev/stdout -l $CRON_LOGLEVEL

My goal is to start this script with CRONJOBS.sh (for testing purpose 1min)

*       *       *       *       *       /share/config/scripts/_cron_1min.sh

cron starts fine but end with error while executing the script

Pseudo-terminal will not be allocated because stdin is not a terminal.
crond: crond (busybox 1.29.3) started, log level 7
/bin/ash: /share/config/scripts/_cron_1min.sh: not found
crond: USER root pid  77 cmd /share/config/scripts/_cron_1min.sh

So I tried to simplify the command.

*       *       *       *       *       pwd

but

crond: USER root pid 76 cmd pwd

/bin/ash: pwd
: not found

I found various posts that the shell of cron has not much env variables so I add this line to dockerfile

RUN printenv | sed 's/^\(.*\)$/export \1/g' > /ENV.sh

in order to change the CRONJOBS.sh to

    *       *       *       *       *       . /ENV.sh; pwd

but again

crond: USER root pid  78 cmd . /ENV.sh; pwd
/bin/ash: pwd
: not found

At least I added this CRONJOBS.sh

* * * * * env > /env.output

Which leads to

USER=root
HOSTNAME=qnap_cron
SHLVL=2
HOME=/root
CRON_LOGLEVEL=7
LOGNAME=root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/ash
PWD=/root

The normal env output is

HOSTNAME=qnap_cron
PWD=/
HOME=/root
TERM=xterm
SHLVL=1
CRON_LOGLEVEL=7
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

For testing I added in docker-compose

            - TERM=xterm

to have the same environment. But again always is everything not found except

env > /env.output

For everyone who reads up to here many thanks. Accordingly to the output it must be as user root under /bin/ash. Every command works as user root on the shell without cron. If anyone has a tip for me I would be very glad about. I really do not get the reason. Please help me.

Do you see this file in the container and is it executable?
And it’s more secure to write the full path to the commands in cron, you can never be sure which part of the environment is available.

Yes, I see the file and it is executable.

/ # ls -la /share/config/scripts/_cron_1min.sh
-rwxrwxrwx    1 1000     users          658 Jun  8 08:12 /share/config/scripts/_cron_1min.sh

The “full-path-problem” should be solved with “. /ENV.sh” (see above) if I understand it right. It seems for me, that commands like “. /ENV.sh” or “source ./ENV.sh” are not parsed and perhaps the from Docker mounted volume ("/share") is not available in cron.

FUCK. Nearly a whole day before I realized that working on CRONJOBS.sh with nano in the console leads to symbols on line ending which are not displayed (in nano).

nano displays

*       *       *       *       *       /share/config/scripts/_cron_1min.sh

the same file opened with vi displays

*       *       *       *       *       /share/config/scripts/_cron_1min.sh^M

After deleting the “^M” at the end on the line with vi it works. Now I have to clean my upchucked keyboard…