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

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.

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.

3 Likes

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

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.

2 Likes

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.

1 Like

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)

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

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

4 Likes

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.

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…

1 Like

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