Cron Job Setup

Hi - I’ve a bit of a blind spot when it comes to cron setup and I’m having some issues with a client site where scheduled posts are not posting.

I understand that WP_cron is disabled by default and that crontab should be setup with
*/5 * * * * curl http://example.com/wp/wp-cron.php
(run the cronjob every 5 mins)

However - using trellis how does one go about actually setting up crontab/ cronjobs?
Is it something that should be setup as part of the ansible tasks?

or do I just add a crontab file(somewhere)?

In roles/wordpress-setup/tasks/main.yaml there is:

    - name: Setup WP system cron
      cron:
        name: "{{ item.key }} WordPress cron"
        minute: "*/15"
        user: "{{ web_user }}"
        job: "curl -k -s {{ site_env.wp_siteurl }}/wp-cron.php > /dev/null 2>&1"
        cron_file: "wordpress-{{ item.key | replace('.', '_') }}"
      with_dict: "{{ wordpress_sites }}"
      when: site_env.disable_wp_cron and not item.value.multisite.enabled | default(false)

Does this just setup the default wp-cron? (which is disabled by default?)
or
does that task actually ping the wordpress cron every 15mins?

any pointers or walkthroughs on how to set up basic cron with trellis/ bedrock would be great!!

Trellis handles WP cron by default when disable_wp_cron is enabled (default), although there’s currently a bug with it:

Thanks. I did think so…

Note: this can’t be merged yet because the --due-now feature is not in the last WP-CLI release so we’re waiting on the next one.

Any indications of how long the wait might be for the release?

No, but you can edit the cron job roles/wordpress-setup/tasks/main.yaml to have the URL of your WP site in the meantime

1 Like

Still having some cron issues reported so just to hold my hand a little here: I’m changing

- name: Setup WP system cron
  cron:
    name: "{{ item.key }} WordPress cron"
    minute: "*/15"
    user: "{{ web_user }}"
    job: "curl -k -s {{ site_env.wp_siteurl }}/wp-cron.php > /dev/null 2>&1"
    cron_file: "wordpress-{{ item.key | replace('.', '_') }}"
  with_dict: "{{ wordpress_sites }}"
  when: site_env.disable_wp_cron and not item.value.multisite.enabled | default(false)

to:

- name: Setup WP system cron
  cron:
    name: "{{ item.key }} WordPress cron"
    minute: "*/15"
    user: "{{ web_user }}"
    job: "curl -k -s mysiteurl.com/wp/wp-cron.php > /dev/null 2>&1"
    cron_file: "wordpress-{{ item.key | replace('.', '_') }}"
  with_dict: "{{ wordpress_sites }}"
  when: site_env.disable_wp_cron and not item.value.multisite.enabled | default(false)

so changing this line:
job: "curl -k -s mysiteurl.com/wp/wp-cron.php > /dev/null 2>&1"

then reprovision server

Hi @chrisk2020 … I tried doing this and reprovision the server.
The flie /etc/cron.d/wordpress_domain_com did changed into this

#Ansible: domain.com WordPress cron */15 * * * * web cd /srv/www/domain.com/current && wp cron event run --due-now > /dev/null 2>&1

But Mailgun is still logging this

Retry limit reached. Dropped: root@mg.domain.com → web@mg.domain.com 'Cron <web@server> cd /srv/www/domain.com/current && wp cron event run --due-now' MX lookup failed

with details json format (truncated)

"message": {
    "headers": {
      "to": "web",
      "message-id": "20160825083006.82493.45993.675EBA03@mg.domain.com",
      "from": "root (Cron Daemon)",
      "subject": "Cron <web@server> cd /srv/www/domain.com/current && wp cron event run --due-now"
    },

The Ansible script now is like this

- name: Setup WP system cron
  cron:
    name: "{{ item.key }} WordPress cron"
    minute: "*/15"
    user: "{{ web_user }}"
    job: "cd {{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }} && wp cron event run --due-now > /dev/null 2>&1"
    cron_file: "wordpress-{{ item.key | replace('.', '_') }}"
  with_dict: "{{ wordpress_sites }}"
  when: site_env.disable_wp_cron and not item.value.multisite.enabled | default(false)

Thanks for your help!

Sorry - I don’t think I ever actually got this resolved - maybe someone else wants to step in? - (I’ve not checked to see if the commit mentioned by @ben has been merged yet)

I see… alright thanks @chrisk2020

I have a multi-site setup on a digital ocean droplet. One of the sites is set with a lot of scheduled posts but they’re not being published, instead they eventually receive the “missed schedule” notice.

My MailGun logs are showing the cron jobs being accepted:

{
"tags": null,
"envelope": {
	"sender": "root@mg.project.com",
	"transport": "smtp",
	"targets": "web@mg.project.com"
},
"storage": {
	"url": "https://se.api.mailgun.net/v3/domains/mg.project.com/messages/AgEFdhOLEztpFnyRqe9NNYSTo9040AkDZA==",
	"key": "AgEFdhOLEztpFnyRqe9NNYSTo9040AkDZA=="
},
"log-level": "info",
"method": "SMTP",
"user-variables": {},
"flags": {
	"is-authenticated": true,
	"is-test-mode": false
},
"recipient-domain": "mg.project.com",
"timestamp": 1604082605.30419,
"id": "eFFLh3drRy-HRRuO1wedpg",
"message": {
	"headers": {
		"to": "web",
		"message-id": "20201030183005.fb9702090570592c@mg.project.com",
		"from": "root (Cron Daemon)",
		"subject": "Cron <web@project> cd /srv/www/project.com/current && wp site list --field=url | xargs -n1 -I % wp --url=% cron event run --due-now > /dev/null 2>&1"
	},
	"size": 669
},
"recipient": "web@mg.project.com",
"event": "accepted"

}

The timestamps in MailGun logs are every 30 minutes and each one says they’ve been accepted. That seems about right based on the settings in roles/wordpress-setup/tasks/main.yml

- name: Setup WP Multisite system cron
  cron:
name: "{{ item.key }} WordPress network cron"
minute: "{{ item.value.cron_interval_multisite | default('*/30') }}"
user: "{{ web_user }}"
job: "cd {{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }} && wp site list --field=url | xargs -n1 -I \\% wp --url=\\% cron event run --due-now > /dev/null 2>&1"
cron_file: "wordpress-multisite-{{ item.key | replace('.', '_') }}"
state: "{{ (cron_enabled and item.value.multisite.enabled) | ternary('present', 'absent') }}"
  with_dict: "{{ wordpress_sites }}"

Does anyone know where I can go from here to get these published? Thanks much!

Hmm, I just noticed this comes up in the MailGun logs quick view tab… not sure what deal is with this.

Ideally I’d like to have posts get published at the exact minute they’re scheduled to publish.

I went forward and updated main.yml to adjust the multisite minutes from 30 to 1 minute. MailGun is showing an accepted cron job every minute now but the posts are still showing that they missed their schedule.

This may be relevant to your setup: