Roots Discourse

Best practice for adding custom cron job on provision


Hi everybody! I’m a newbie in server setup, i would like to know what’s the best practise on setting a custom cron job to trellis.
I’m playing with the wordpress-setup role and adding custom cron tasks to “/tasks/main.yml but i’d like to use a bash script to manage the cron job … something like job=”/some/"
the script should execute a “wp db export” command to backup daily the wordpress db…
Where can i put the bash script on the server? is it safe to put in the bedrock root directory or better in the “web” user home? can’t figure out how to handle in roots workflow also…:sweat_smile:

Sorry for my english and thanks to roots team for their work
really exciting project :astonished:

Permissions when adding a custom cron job on deploy (permission denied)

I’d probably add the cron job as part of the deploy.

This means you could use the deploy hooks to run a custom task. Maybe on deploy_after.

Ansible has a cron module so use that and also use Ansible to put that file on the server. Or use a template so you have access to all the Trellis variables.

If you don’t want to add it as part of deploys, you can just modify wordpress-setup like you said or create a new role.


If you got things up and running could you go through the process here? Thanks!


I just set this up myself and it works quite well. Here’s what you do:

Add the following to roles/wordpress-setup/tasks/main.yml:

- name: Setup custom system cron
    name: "{{ item.key }} site cron"
    hour: "{{ item.value.cron.hour | default('*') }}"
    minute: "{{ item.value.cron.minute | default('*') }}"
    user: "{{ web_user }}"
    job: "cd {{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }} && {{ item.value.cron.cmd }} > /dev/null 2>&1"
    cron_file: "custom-{{ item.key | replace('.', '_') }}"
  with_dict: "{{ wordpress_sites }}"
  when: item.value.cron | default(false)

Then in your /group_vars/*/wordpress_sites.yml file add the following to the site you want to set the custom cron job:

      cmd: "./bin/" # the cron tab will set the directory to your site root and then execute this command
      hour: "*" # Runs every hour. Change this if you want to run at a specific hour
      minute: "0" # run at minute 0 (once per hour)

Re-provision Trellis and you should be good to go!

Refer to Ansible’s Cron Module for more info:


Hi there, my name is Jed,
also new on Ansible.

Anyway, I have a problem when adding multiline jobs with Ansible.
I have tried everything on the ansible cron_module but nothing is work.

name: “Activedefrag”
minute: “*”
hour: “3”
job: “/bin/bash /root/”
state: present
tags: redis_activedefrag_cron

name: “Deactivedefrag”
minute: “*”
hour: “5”
job: “/bin/bash /root/”
state: present
insertafter: “Deactive”
tags: redis_activedefrag_cron

what I expected is something like this

#Ansible: Activedefrag
*3 * * * /bin/bash /root/
#Ansible: Deactivedefrag
*5 * * * /bin/bash /root/

any suggestion?

Thank you!