Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Node.js Cron

Hello Everyone I just about have my entire app dockerized except my cron jobs here is my dockerFile

FROM nodesource/precise

# Update install os dep
RUN apt-get update && apt-get install -y apt-utils cron
RUN apt-get -y install pwgen python-setuptools curl git unzip vim

# Add code
RUN mkdir /var/sites
ADD /api /var/sites/api
ADD /services /var/sites/services
RUN cd /var/sites/services && npm install
RUN cd /var/sites/api && npm install

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

my cron file

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
* * * * * cd /var/sites/services/ldapSync && node index.js >> 2>&1
# An empty line is required at the end of this file for a valid cron file.

if I remove the node cron job just leave the hello world it works fine but when I have the node cron in there it doesn't appear to do anything. If I go into the container and do crontab -e and add it manually it works fine.

Any ideas what I'm doing wrong?

Thanks

like image 653
MrB Avatar asked Sep 01 '26 12:09

MrB


1 Answers

In second line of your cron file you are missing username in the format

So instead of

* * * * * cd /var/sites/services/ldapSync && node index.js >> 2>&1

you should have

* * * * * root cd /var/sites/services/ldapSync && node index.js >> 2>&1

For more info see this

like image 158
hlihovac Avatar answered Sep 05 '26 16:09

hlihovac