Search code examples
bashdockercron

Bash shell script not executing command docker entrypoint


I have a shell script, it is not executing the "crontab" command when it is run from docker CMD. But works when I execute the file inside the docker container. What am I missing?

File entry.sh:

#!/bin/sh
echo "Starting cron"
/usr/sbin/cron -f -L 15
crontab /etc/cron.d/cron-python

Docker file:

FROM python:3
RUN apt-get update && apt-get -y install cron
COPY ./cron/entry.sh /entry.sh
COPY ./cron/crontab.txt /etc/cron.d/cron-python
RUN chmod +x /entry.sh
RUN chmod 0644 /etc/cron.d/cron-python
CMD ["./entry.sh"]

cron-python:

#   crontab -e
*/1 * * * * python /app/myscript.py >> /var/log/script.log 2>&1

Solution

  • Following should work :

    #!/bin/sh
    # start cron
    echo "Starting cron"
    crontab /etc/cron.d/cron-python
    /usr/sbin/cron -f -L 15
    

    In your version, /etc/cron.d/cron-python does not get installed as you asked /usr/sbin/cron to run on foreground.

    Also you need to copy /app/myscript.py.