0

I am trying to achieve run a shell script periodically in a docker container with crontab. Dockerfile:

FROM ubuntu:latest

RUN apt-get update && apt-get -y install cron nano

RUN mkdir -p /custom/bin

COPY ./myscript.sh /custom/bin/myscript.sh

RUN chmod +x /custom/bin/myscript.sh

COPY ./dockerconfigs/crontab /etc/cron.d/crontab

RUN chmod 0644 /etc/cron.d/crontab

RUN crontab /etc/cron.d/crontab
 
RUN touch /var/log/cron.log

CMD cron && tail -f /var/log/cron.log

myscript.sh:

#!/bin/bash
echo "test" > /test.txt
touch /test2.txt

crontab:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /custom/bin/myscript.sh
* * * * * touch /abc.txt

I can see that 2. crontab is running successfully. I can see /abc.txt file on every minute. But it seems like myscript.sh is not working. I can not see any /test.txt or /test2.txt file. Also there is nothing in the /var/log/cron.log file. I've tried with different base image (php:7.2-cli) but no luck.

Also I can confirm that my script is working when I try to run it on command line.

PS: I can see 'abc.txt'$'\r' instead of abc.txt file in folders. I am using windows 10 and docker desktop.


Adi Soyadi
  • 23
  • 6
  • 1
    The DOS-style line endings will cause problems. If the "shebang" line is actually `#!/bin/bash\r` then the system won't find an interpreter containing the extraneous newline character. See for example [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings). – David Maze Aug 15 '23 at 16:59

1 Answers1

0

Thanks to David Maze. I realized that I have issue with special characters. I just added following line to the Dockerfile and fixed:

RUN sed -i 's/\r$//' /etc/cron.d/crontab
Adi Soyadi
  • 23
  • 6