# How to add a job to cron/anacron when creating Docker container

**URL:** https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194
**Category:** Self-hosting
**Created:** [17 ביולי,‏ 2019,‏ 1:20pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194 "2019-07-17T13:20:29Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Owen\_Neuber](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@Owen\_Neuber](https://meta.discourse.org/u/Owen_Neuber)
#### Post date: [17 ביולי,‏ 2019,‏ 1:20pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/1 "2019-07-17T13:20:29Z")

</div>

I am trying to add a simple cron job to run in my Docker container. I want it to be added during setup so it will automatically run when `./launcher rebuild app` completes.

The cron job works when I manually add it to the `/etc/cron.d/anacron` file with `vim` but if I edit it without entering and saving the file (i.e. with `echo -n '* * * * * root python3 /var/www/discourse/script/email_me.py' >> /etc/cron.d/anacron`), cron does not seem to pickup the change and does not run the job.  
If I then add a space anywhere in the file with `vim` and save, cron will pickup the change and start running the job.  
Since the changes are not picked up when adding them with the `echo` command, adding this command to my `app.yml` file does not work.  
I also tried `docker restart app` after echoing in the changes which still did not work.

I feel like there should be an easier way that I’m missing. Having to edit the anacron file after setup seems like the wrong way to do this.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [17 ביולי,‏ 2019,‏ 2:57pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/2 "2019-07-17T14:57:19Z")

</div>

Can you explain in more detail what you are trying to do here?

You will likely have a much easier time making a plugin for your forum that runs on an interval in sidekiq.

---

<div class="post-metadata">

### Author: ![Owen\_Neuber](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@Owen\_Neuber](https://meta.discourse.org/u/Owen_Neuber)
#### Post date: [17 ביולי,‏ 2019,‏ 3:23pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/3 "2019-07-17T15:23:39Z")

</div>

Thanks for getting back to me! Maybe this will make more sense:

- I am running a privately hosted discourse repo with some custom files on it.
- I have a file in Discourse’s `script` directory called `email_me.py`
- When run, this file sends me an email saying “Hello World!”
- I want to create a cron job in the Discourse Docker container that will run this file every minute.
- This can be accomplished by manually adding the line `* * * * * root python3 /var/www/discourse/script/email_me.py` to the `/etc/cron.d/anacron` file using an editor like `vim`.
- I want the above cron job to run when the Discourse container is set up, however I cannot use `vim` to do so from the `app.yml` file.
- I tried using `echo -n '* * * * * root python3 /var/www/discourse/script/email_me.py' >> /etc/cron.d/anacron` as a command in the `app.yml` file. This successfully adds the line to the anacron file but the job does NOT run (as if the change was not registered).

I am simply trying to find a way to have the job:  
`* * * * * root python3 /var/www/discourse/script/email_me.py`  
run after rebuilding with `./launcher rebuild app`

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [17 ביולי,‏ 2019,‏ 3:37pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/4 "2019-07-17T15:37:33Z")

</div>

You should **really** follow @eviltrout advice and do this properly in a plugin using our Sidekiq scheduled jobs.

But if you want to mess with those **unsupported** changes to cron, you will probably need to restart the service after adding a job with `sv restart cron`.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [17 ביולי,‏ 2019,‏ 3:47pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/5 "2019-07-17T15:47:18Z")

</div>

Also to be more clear, I am curious what your end goal is, not the steps you are using to achieve it. If the goal is to have an email sent to you once a minute, you can do that in a sidekiq job in a plugin and it’s significantly simpler.

---

<div class="post-metadata">

### Author: ![Owen\_Neuber](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@Owen\_Neuber](https://meta.discourse.org/u/Owen_Neuber)
#### Post date: [17 ביולי,‏ 2019,‏ 3:59pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/6 "2019-07-17T15:59:04Z")

</div>

My end goal is to run a database query looking for specific user activity and send an email with a table containing the results. This email will act as a pre-payment confirmation as the users who did this specific activity will be paid later in the day (via PayPal) through another script run by cron.

I already have scripts that can do both these things but I just need to find a way to get the discourse container to run them once per week.

I am not familiar with Sidekiq. If it can do this, where should I look to learn it?

Thanks!

(Also I tried `sv restart cron` and many other ways of restarting cron to no avail)

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [17 ביולי,‏ 2019,‏ 4:12pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/7 "2019-07-17T16:12:04Z")

</div>

I would begin with the plugin tutorial series for the basic structure of a plugin:

> [@Developing Discourse Plugins - Part 1 - Create a basic plugin](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-1/30515):
>
> Building a plugin in Discourse can be really simple, once you learn a couple of quirks. The goal of this post is to create a skeleton plugin and introduce you to the basics. Your development environment Make sure you have a development environment of Discourse running on your computer. I recommend you use [the appropriate setup guide](https://meta.discourse.org/tag/dev-install) and come back when you’re done. plugin.rbtada Use [GitHub - discourse/discourse-plugin-skeleton: Template for Discourse plugins · GitHub](https://github.com/discourse/discourse-plugin-skeleton) to create a complete di…

If you want an example of how to add a job to a plugin the discourse-assign plugin has one that sends periodic reminders:

[https://github.com/discourse/discourse-assign/blob/master/jobs/scheduled/enqueue\_reminders.rb](https://github.com/discourse/discourse-assign/blob/master/jobs/scheduled/enqueue_reminders.rb)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [17 ביולי,‏ 2019,‏ 5:53pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/8 "2019-07-17T17:53:52Z")

</div>

You should write a proper plugin as suggested, but here’s how to do what you asked.

You can add something like this in the `run` section near the end of `app.yml`.

```
  - exec: cp /shared/rr/cron.d.rr /etc/cron.d/rr

```

You want to put both your script and this file in some directory that’s mapped under `shared` rather than in the discourse directory.

---

<div class="post-metadata">

### Author: ![Owen\_Neuber](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@Owen\_Neuber](https://meta.discourse.org/u/Owen_Neuber)
#### Post date: [17 ביולי,‏ 2019,‏ 6:24pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/9 "2019-07-17T18:24:07Z")

</div>

Yes that worked perfectly. I had to create the `rr` folder and `cron.d.rr` files. I put my cron job in the `cron.d.rr` file and the `cp` worked.

Thank you!

Now to look into writing a proper plugin…

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [16 באוגוסט,‏ 2019,‏ 6:24pm UTC](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194/10 "2019-08-16T18:24:13Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
