Can someone explain to me how cron jobs work in Trellis?

I created a new cusotm WP CLI command, but I did not use the wp_schedule_event or anything. When I deployed my new code to staging, it started running this cron job every 15 minutes when the default trellis wp-cron runs.

Why is that? While that’s the schedule I’ll probably want this running on, I have another WP CLI command I am going to create I want run once a day. How do I set the schedule for two new commands I am adding to WP CLI?

This is all Trellis does for cron: https://github.com/roots/trellis/blob/2597bdd4ca220cb1af3f69ea9d27698f90d46119/roles/wordpress-setup/tasks/main.yml#L28-L36

1 Like

Found the confusion. Someone had setup a cron to be executed under another user outside the typical trellis workflow.

Hi @swalkinshaw and devs.

I am struggling a little bit with Cron because I’m using a plugin that seems really great called WP Ultimate CSV Importer Pro to import/update some CPTs based on a CSV that a little python script is converting from a Google Spreadsheet, run nightly via cron.

I want to enable Cron on the Staging Server for testing purposes which I have done by adding the following line to group_vars/staging/wordpress_sites.yml:

wordpress_sites:
    example.com:
        disable_wp_cron: false

And then running

ansible-playbook server.yml -e env=staging --tags "wordpress-setup"

Is this correct?

I see that the vault_wordpress_sites[site].env, project.env and wordpress_env_defaults in roles/deploy/vars/main.yml are merged to generate the site_env dictionary:

site_env: "{{ wordpress_env_defaults | combine(project.env | default({}), vault_wordpress_sites[site].env) }}"

But I’m still a bit unclear on where project.env and vault_wordpress_sites[site].env are set.

I ran this command on the server to check Cron logs:

sudo grep cron /var/log/syslog

Output contains:

Aug  5 16:36:28 ubuntu-s-1vcpu-1gb-etc ansible-cron: Invoked with name=example.com nightly Import File insertbefore=None job=cd /srv/www/example.com/current/scripts && ./convert_google_docs.py --filename imported_file.csv --path /srv/www/example.com/current/web/app/uploads/ > /dev/null 2>&1 cron_file=update-district-leaders-example_com reboot=False hour=0 month=* disabled=False state=present special_time=None user=web env=None insertafter=None backup=False day=* minute=0 weekday=*
Aug  5 16:45:01 ubuntu-s-1vcpu-1gb-etc CRON[22171]: (web) CMD (cd /srv/www/example.com/current && wp cron event run --due-now > /dev/null 2>&1)

Enable which cron? By default Trellis uses system cron instead of the built-in WP one. If you want system cron, you don’t need to do anything. So I’m assuming you want WP cron instead?

As for the other questions:

project.env is what you set in group_vars/staging/wordpress_sites.yml.

vault_wordpress_sites[site].env comes from the equivalent site in the vault file: group_vars/staging/vault.yml.

So we start with the defaults, then merge in your project specific ones, then merge in any ones in the vault as the final step.

Anyway, judging from that log output, it looks like system cron is still running. Assuming those timestamps are after you provisioned again?

This should result in absent. However, I’m not sure we’re restarting cron (or if we need to) :thinking:. You can try service crond restart or whatever it is.

Thank you, @swalkinshaw!

Yes I can confirm System Cron is running:

$ sudo service cron status
● cron.service - Regular background program processing daemon

They plugin dev recommends editing the crontab to ping the wp_cron.php file:

* 1 * * * wget https://staging.example.com/wp/wp-cron.php

Since /etc/cron.d/wordpress-example.com exists and contains:

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

We should expect that wp-cron is being manually run every fifteen minutes from system cron, right?

I’m going to install wpackagist-plugin/wp-crontrol and see if that reveals anything.

Now I’m just confused. Before you were trying to set disable_wp_cron: false.

If the plugin dev is recommending using crontab anyway, why are you disabling it? Trellis’ default should be fine and you already had it working fine (judging from your logs).

Yes, Trellis runs the WP cron every 15 minutes.

Dude, I’m the one who’s confused! I think i get now that when we talk about disabling the Wordpress Cron, we mean usually mean disabling whatever php-based timing system WP uses by default and triggering WP Cron tasks from the Server Cron. Maybe some people just run all the WP Cron manually.

At any rate, I think I get it now, thanks you you, as usual.

Man, is Ansible/Trellis dope! Loving tags.

I can confirm that their cron events can run succesfully:

$ wp cron event run --due-now
Executed the cron event 'smack_uci_cron_scheduler' in 0.005s.
Executed the cron event 'smack_uci_cron_scheduled_export' in 0.002s.
Executed the cron event 'smack_uci_email_scheduler' in 0.003s.
Executed the cron event 'smack_uci_replace_inline_images' in 0.003s.
Executed the cron event 'smack_uci_image_scheduler' in 0.003s.

So I don’t think it’s my server configuration. Now just need to figure out what is wrong.

1 Like