Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron job is not working #65

Closed
RammusXu opened this issue Sep 12, 2018 · 11 comments
Closed

Cron job is not working #65

RammusXu opened this issue Sep 12, 2018 · 11 comments

Comments

@RammusXu
Copy link

RammusXu commented Sep 12, 2018

Hi,
I have same problem as bottom of #4.
I use docker-compose up with the config below.

version: '3'

services:
  mautic:
    image: mautic/mautic
    links:
      - mauticdb:mysql
    environment:
      - MAUTIC_DB_HOST=mysql:3306
      - MAUTIC_DB_PASSWORD=xxxx
  
  mauticdb:
    image: mysql:5.7
    volumes:
      - ./.data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: xxxx
      
networks:
  default:
    external:
      name: internal

I try to get cron job information with commands.

root@7ae7104b05dd:/etc/cron.d# crontab -l                                                  
no crontab for root
root@7ae7104b05dd:/etc/cron.d# crontab -u www-data -l                                      
no crontab for www-data
root@7ae7104b05dd:/var/www/html# service cron status
[ ok ] cron is running.

Then, I try to run mautic commnad as user www-data. Found some error here!

root@b9e1b7c2fcdf:/var/www/html# su www-data -s /bin/sh
$ php /var/www/html/app/console mautic:segments:update

Warning: fopen(/var/www/html/app/cache/prod/../run/sf.mautic-segments-update.5415eaf7823c0a036b252ba8a9f1f4065263e1c60a23cacf6b5851144245b8c9.lock): failed to open stream: Permission denied in /var/www/html/app/bundles/CoreBundle/Command/ModeratedCommand.php on line 150

Warning: flock() expects parameter 1 to be resource, boolean given in /var/www/html/app/bundles/CoreBundle/Command/ModeratedCommand.php on line 151
Failed to lock /var/www/html/app/cache/prod/../run/sf.mautic-segments-update.5415eaf7823c0a036b252ba8a9f1f4065263e1c60a23cacf6b5851144245b8c9.lock.
Script in progress. Can force execution by using --force.
@RammusXu
Copy link
Author

@luizeof Do you have any ideas? Please help me.

@proffalken
Copy link

@RammusXu Please can you re-run the above commands, but this time when you get the permissions error, can you run ls -l against the file in the fopen() brackets and paste the input here?

@RammusXu
Copy link
Author

root@10e257dca355:/var/www/html/app/cache/run# ls -la /var/www/html/app/cache/prod/../run/sf.mautic-segments-update.5415eaf7823c0a036b252ba8a9f1f4065263e1c60a23cacf6b5851144245b8c9.lock
ls: cannot access '/var/www/html/app/cache/prod/../run/sf.mautic-segments-update.5415eaf7823c0a036b252ba8a9f1f4065263e1c60a23cacf6b5851144245b8c9.lock': No such file or directory

root@10e257dca355:/var/www/html/app/cache/run# ls -la
total 8
drwxr-xr-x 2 root     root     4096 Sep 18 09:32 .
drwxr-xr-x 5 www-data www-data 4096 Sep 12 10:34 ..

root@10e257dca355:/var/www/html/app/cache/run# ls -la  /var/www/html/app/bundles/CoreBundle/Command/ModeratedCommand.php
-rw-r--r-- 1 www-data www-data 6898 Jul 25 17:17 /var/www/html/app/bundles/CoreBundle/Command/ModeratedCommand.php

Is it clearly?

@proffalken
Copy link

hmmm, ok.

The offending code is https://github.com/mautic/mautic/blob/staging/app/bundles/CoreBundle/Command/ModeratedCommand.php#L150 so it's a case of tracking back where this is being called from and going from there

@luizeof
Copy link
Member

luizeof commented Sep 18, 2018

@RammusXu

Mautic Docker uses crontab in the /etc/cron.d/ directory, so it does not appear in crontab -l.

It does not make much sense to run the commands as root because they can write the log and the www-data apache user can not read files created by root.

About the error:

Warning: fopen(/var/www/html/app/cache/prod/../run/sf.mautic-segments-update.5415eaf7823c0a036b252ba8a9f1f4065263e1c60a23cacf6b5851144245b8c9.lock): failed to open stream: Permission denied in /var/www/html/app/bundles/CoreBundle/Command/ModeratedCommand.php on line 150

Make sure the app / cache folder was created by root and set the property to www-data.

@t-l-k
Copy link

t-l-k commented Oct 11, 2018

@luizeof

Make sure the app / cache folder was created by root and set the property to www-data.

What does this mean, set the property to www-data?

Am also debugging this container now and /etc/cron.d/mautic does not appear to be running, despite cron service status reporting ok - nothing coming through on the fifo /var/log/cron.pipe.

Have checked cross referenced with Debian stretch cron documentation and it appears to be compliant. Will look in more detail tomorrow myself but ...

... has anyone progressed this at all? This image doesn't appear to have any syslogd enabled.

@jesseshieh
Copy link

I edited /etc/cron.d/mautic and commented out the line starting with @reboot and the jobs seem to be working for me now (except the @reboot command of course). I haven't dug into exactly why @reboot doesn't work, but here are some hints to investigate.
https://unix.stackexchange.com/questions/109804/crontabs-reboot-only-works-for-root

Also, I'm not sure if a reboot command makes sense for a docker container.

@t-l-k
Copy link

t-l-k commented Oct 15, 2018

Thanks @jesseshieh , I will try and follow suit. I'm not totally convinced my workstation is not mangling CRLF/LF correctly, but the Debian stretch cron documentation does say that cron is very fussy with the final line in the file and the presence of a LF vs an EOL, so that might be worth checking as well.

@t-l-k
Copy link

t-l-k commented Oct 16, 2018

I've just tried taking the mautic.crontab file, ensuring VSCode reports endings as LF, saved it and copied it into my image in a derived dockerfile overwriting the one from the published docker-mautic image, and it is now working. This is with the @reboot command enabled. I docker cp'd the files out from the original mautic/mautic image, and they're identical, line endings and all.

Digging a little deeper, the only difference I can see is the permissions applied to the file, essentially chmod 755?

Debian stretch docs say that 644 is an appropriate setting though:

cron.other files:

the files under these directories have to be pass some sanity checks including the following: be executable, be owned by root, not be writable by group or other and, if symlinks, point to files owned by root.

cron.d files, as above and:

Files in this directory have to be owned by root, do not need to be executable (they are configuration files, just like /etc/crontab) and must conform to the same naming convention as used by run-parts(8)

Working:

  File: /etc/cron.d/mautic
  Size: 1793            Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d      Inode: 1183275     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 13:37:33.651217100 +0000
Modify: 2018-10-16 13:35:09.000000000 +0000
Change: 2018-10-16 13:35:34.063797300 +0000

Wasn't working:

  File: /etc/cron.d/mautic
  Size: 1793            Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d      Inode: 3160573     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-08-14 17:37:35.000000000 +0000
Modify: 2018-08-14 17:37:35.000000000 +0000
Change: 2018-09-13 14:38:41.453996500 +0000

Something seems inconsistent, perhaps it is an issue with the PHP base image.

@elwood218
Copy link

For me the cron was not working too but because of another reason. I have build my own Docker Image with Gitlab-CI and somehow the permissions at cloning of the repo changed to 666.
The cron-file is not allowed to have write-permission on group or other.
https://debian-administration.org/article/687/So_your_cronjob_did_not_run

So I have added this to my Dockerfile after the COPY of mautic.crontab and now it is working again:

# Ensure cron file has correct permissions
RUN chmod 644 /etc/cron.d/mautic

@mautibot
Copy link

This issue has been mentioned on Mautic Community Forums. There might be relevant details there:

https://forum.mautic.org/t/how-to-check-mautic-cron-jobs-inside-docker-container/19792/3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants