2

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"]
Gavin Teo Juen
  • 310
  • 5
  • 18
  • 1
    Consider using a [`docker`-friendly](https://stackoverflow.com/a/75353647/52499) `cron` implementation. – x-yuri Feb 05 '23 at 16:32

3 Answers3

4

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"]

Gavin Teo Juen
  • 310
  • 5
  • 18
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.

Azarro
  • 1,776
  • 1
  • 4
  • 11
  • i try to add two different commands `RUN echo ' * * * * * /usr/local/bin/feeds.sh' > /etc/crontabs/root RUN echo ' */5 * * * * /usr/local/bin/reminders.sh' > /etc/crontabs/root `. For reminders.sh, it did run every 5minutes. However , the cron jobs does not work for feeds.sh. It only show reminder.sh cronjobs every 5minutes – Gavin Teo Juen May 07 '22 at 11:41
  • 1
    @GavinJuen You need to use >>, not >. `>` will overwrite the whole crontab. >> will append – Azarro May 07 '22 at 11:42
  • Thank you so much it helps!!! i will upvote your answer – Gavin Teo Juen May 07 '22 at 11:57
  • 1
    Awesome, glad it's working! Hope your project goes well! – Azarro May 07 '22 at 11:57
  • @Azzaro i posted another question regarding my project. I wonder if u could have look at it. Thank you – Gavin Teo Juen May 08 '22 at 06:12
1

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.

x-yuri
  • 16,722
  • 15
  • 114
  • 161