Search code examples
pythondockercentoscron

crontab not working on Centos Docker container


I am trying to run a few python scripts through Crontab in Centos docker container, but nothing I have tried is working.

Firstly I installed cron:

yum install vixie-cron

Then I ran it as a service:

/etc/init.d/crond start

(I also ran /sbin/service crond start because some answers to related questions suggested so)

ps aux | grep cron shows:

root     16917  0.0  0.0  23288  1252 ?        Ss   18:53   0:00 crond                                                                                
root     16929  0.0  0.0   9720   836 pts/0    S+   18:55   0:00 grep cron 

crontab -l looks like:

0 17 1 * * /root/proj/env/bin/python /root/proj/files/frontend/file1.py > /var/log/cron.log                                                      

0 9 4 * * /root/proj/env/bin/python /root/proj/files/frontend/file2.py > /var/log/cron.log                                                      

0 17 15 * * /root/proj/env/bin/python /root/proj/files/frontend/file3.py > /var/log/cron.log                                                     

0 9 18 * * /root/proj/env/bin/python /root/proj/files/frontend/file4.py > /var/log/cron.log                                                     

0 14 * * * /root/proj/env/bin/python /root/proj/files/frontend/file5.py > /var/log/cron.log                                               

0 8 * * * /root/proj/env/bin/python /root/proj/files/frontend/file6.py > /var/log/cron.log                                                  

* * * * * echo 'Check!!' > /var/log/cron.log 

All python scripts and the cron.log file have permission 777 The last entry in crontab is just to check if anything is getting written to the log file... but nothing is being written there..

Any idea how to solve this?

PS: I looked through a ton of related QnAs but none of them helped.

Some answers had suggested writing to /etc/crontab.. so I even made the entry: * * * * * root echo 'Blah' > var/log/cron.log there.. but to no effect :(


Solution

  • After a day wasted in suboptimal Googling and experimentation, the answer to this conundrum was commenting out the following line in the file /etc/pam.d/crond (and then running service crond restart):

    session required pam_loginuid.so
    

    This is because of some Docker-Centos security issue. I don't have further details on why this works. (Check this for details.)

    Also, troubleshooting this made me realize a few gotchas which might be useful:

    • Check that you have crond running as a service (if not, use /etc/init.d/crond start)

    • Each line in your crontab should be followed be a newline character

    • Your scripts and log file should have suitable write/execute permissions

    • crontab file shouldn't have weird Windows environment characters (like ^M)

    • In some rare cases, crond might have different time zone than your system

    • Also check /etc/cron.allow and /etc/cron.deny files to verify who can add/edit cron jobs

    May your day be invested in more fruitful pursuits, stranger.