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/job.sh"
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:

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 job.sh 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.

2 Likes

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
  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:

    cron:
      cmd: "./bin/command-to-run.sh" # 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: http://docs.ansible.com/ansible/cron_module.html

4 Likes

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.

-cron:
name: “Activedefrag”
minute: “*”
hour: “3”
job: “/bin/bash /root/activedefrag.sh”
state: present
tags: redis_activedefrag_cron

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

what I expected is something like this

#Ansible: Activedefrag
*3 * * * /bin/bash /root/activedefrag.sh
#Ansible: Deactivedefrag
*5 * * * /bin/bash /root/deactivedefrag.sh

any suggestion?

Thank you!

Just create two diffenent tasks, like

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

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

@swalkinshaw I’m just curious, when I try to add a cron job as part of the deploy (deploy_after) like you suggested, I’m getting a permission denied error:

IOError: [Errno 13] Permission denied: '/etc/cron.d/BackWPup-Job-1'

But it works fine as a provisioning task/role. Any thoughts on this?

The deploy playbook uses the more limited web user while the full provision playbook uses root/admin user.

web doesn’t have the necessary permissions to set up cron files.

1 Like