Docker Crons
Crontab
Use crontab -l
to verify the cron settings:
# crontab -l
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
* * * * * cd /var/www/scripts && /usr/bin/php /var/www/scripts/cronjobphp.php | tee -a /var/log/cron.log >/proc/1/fd/1 2>/proc/1/fd/2
php script via cron root
- Add a PHP script to root crontab.
- Log the output to
/var/log/cron.log
. - NOTE: For PHP (and maybe everything) You must first
cd
to the directory before running the file to keep relative paths working as expected with cron. You must also envolk PHP from its path/usr/bin/php
, and not via just usingphp
.
dockerfile
RUN echo "* * * * * cd /var/www/scripts && /usr/bin/php /var/www/crons/cronjobphp.php >> /var/log/cron.log" >> /etc/crontabs/root
/var/www/crons/cronjobphp.php
<?php
$myDate = date("Y-m-d h:i:s");
echo "cronjobphp - current date and time: " . $myDate . "\n";
Running cat /var/log/cron.log
shows the results:
cronjobphp - current date and time: 2025-01-09 12:05:00
cronjobphp - current date and time: 2025-01-09 12:06:00
cronjobphp - current date and time: 2025-01-09 12:07:00
cronjobphp - current date and time: 2025-01-09 12:08:00
If cron is running as a secondary (background) process in the docker container, to get the log to output to the dockerlog you need to specifically send it there.
dockerfile
RUN echo "* * * * * cd /var/www/scripts && /usr/bin/php /var/www/crons/cronjobphp.php >/proc/1/fd/1 2>/proc/1/fd/2" >> /etc/crontabs/root
Also, you can log to a file and dockerlog at the same time.
dockerfile
RUN echo "* * * * * cd /var/www/scripts && /usr/bin/php /var/www/crons/cronjobphp.php | tee -a /var/log/cron.log >/proc/1/fd/1 2>/proc/1/fd/2" >> /etc/crontabs/root
sh script via cron root
- Add an SH script to root crontab.
- Log the output to
/var/log/cron.log
.
dockerfile
RUN echo "* * * * * sh /var/www/crons/cronjob1.sh >> /var/log/cron.log" >> /etc/crontabs/root
var/www/crons/cronjob1.sh
#!/bin/sh
echo "cronjob1 - current date and time: $(date)"
Running cat /var/log/cron.log
shows the results:
Current date and time: Wed Jan 8 16:16:31 MST 2025
Current date and time: Wed Jan 8 16:17:00 MST 2025
Current date and time: Wed Jan 8 16:18:00 MST 2025
Current date and time: Wed Jan 8 16:19:00 MST 2025
Current date and time: Wed Jan 8 16:20:00 MST 2025
Current date and time: Wed Jan 8 16:21:00 MST 2025
sh script cron via periodic dir
- Add an SH script to the
/etc/periodic/1min
directory. - Log the output to
/var/log/cron.log
. - NOTE: sh files in a
periodic
directory must NOT have the.sh
extension.
dockerfile
RUN mkdir /etc/periodic/1min
COPY crons/cronjob1.sh /etc/periodic/1min/cronjob1
RUN chmod +x /etc/periodic/1min/cronjob1
RUN echo "* * * * * run-parts /etc/periodic/1min >> /var/log/cron.log" >> /etc/crontabs/root
var/www/crons/cronjob1.sh
#!/bin/sh
echo "cronjob1 - current date and time: $(date)"
Running cat /var/log/cron.log
shows the results:
Current date and time: Wed Jan 8 16:16:31 MST 2025
Current date and time: Wed Jan 8 16:17:00 MST 2025
Current date and time: Wed Jan 8 16:18:00 MST 2025
Current date and time: Wed Jan 8 16:19:00 MST 2025
Current date and time: Wed Jan 8 16:20:00 MST 2025
Current date and time: Wed Jan 8 16:21:00 MST 2025