skip to Main Content

I want to perform cron jobs every minute for feeds.sh and every 5 minutes for reminder.sh inside docker container. It was able to run every minutes for feeds.sh. However for reminder.sh, it cannot run every 5 minutes as it keep throwing the error /bin/ash: */5: not found inside the docker container.

The following code is shown as below :

FROM alpine:latest

# Install curlt 
RUN apk add --no-cache curl

# Copy Scripts to Docker Image
COPY reminders.sh /usr/local/bin/reminders.sh
COPY feeds.sh /usr/local/bin/feeds.sh


# Add the cron job
RUN echo ' *  *  *  *  * /usr/local/bin/feeds.sh &&  */5  *  *  *  *    /usr/local/bin/reminders.sh' > /etc/crontabs/root

# Run crond  -f for Foreground 
CMD ["/usr/sbin/crond", "-f"]

3

Answers


  1. Chosen as BEST ANSWER

    Updated Docker File to run both every minute and every 5 minutes

    FROM alpine:latest
    
    # Install curlt 
    RUN apk add --no-cache curl
    
    # Copy Scripts to Docker Image
    COPY reminders.sh /usr/local/bin/reminders.sh
    COPY feeds.sh /usr/local/bin/feeds.sh
    
    # Add the cron job
    
    RUN echo ' *  *  *  *  * /usr/local/bin/feeds.sh' >> /etc/crontabs/root
    RUN echo ' */5  *  *  *  * /usr/local/bin/reminders.sh' >> /etc/crontabs/root
    
    # Run crond  -f for Foreground 
    CMD ["/usr/sbin/crond", "-f"]
    
    

  2. Add it on a separate line.

    When you use && with cron, it’s expecting multiple cron jobs to add for the same cron frequency.

    eg.

    0 * * * * a && b

    Hence why it says "*/5 not found" because that’s the b above – it thinks it’s a cron script to run.

    Add your */5 * * * * script on a separate line in its own command.

    Login or Signup to reply.
  3. docker-compose.yml:

    services:
      supercronic:
        build: .
        command: supercronic crontab
    

    Dockerfile:

    FROM alpine:3.17
    RUN set -x 
        && apk add --no-cache supercronic shadow 
        && useradd -m app
    USER app
    COPY crontab .
    

    crontab:

    */5 * * * * date
    
    $ docker-compose up
    

    More info in my other answer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search